我使用了一個while循環來顯示Dropdown yes和no,它顯示每個頁面的用戶權限,所以頁面名稱和是或否沒有爲每個頁面下拉。PHP Posting last dynamic表格
表單顯示正確,併爲每個用戶顯示正確的數據庫值作爲默認值。但是,如果我將頁面權限之一從「是」更改爲「否」或僅更新數據庫中的最後一個下拉列表,而其他頁面權限則更改。任何想法我在這裏失蹤?
<?php
$sql = "SELECT DISTINCT username FROM users ORDER BY username";
$resultusers = $connect->query($sql);
?>
<?php
require ("cw/connect.php");
?>
<?php
if(isset($_POST['selectbutton']))
{
$username = $_POST['selectuser'];
$query = "SELECT
users.username, users.first, users.last, users.id, permissions.PermID, permissions.PermUserID, permissions.PermPagesID, permissions.view, pages.PagesName, pages.PagesLink, pages.PagesID
FROM users INNER JOIN permissions ON users.id = permissions.PermUserID INNER JOIN pages ON permissions.PermPagesID = pages.PagesID WHERE username = ?";
$stmt = mysqli_prepare($connect, $query);
if($stmt){
//Put whats to be binded from the statement so in this case we want data for this username selected
mysqli_stmt_bind_param($stmt,"s",$username);
// here add all the varibles to be pulled from database
mysqli_stmt_bind_result($stmt, $username, $first, $last, $id, $PermID, $PermUserID, $PermPagesID, $view, $PagesName, $PagesLink, $PagesID);
mysqli_stmt_execute($stmt);
mysqli_stmt_fetch($stmt);
?>
<b>First:</b><?php echo $first; ?><br>
<b>Last:</b><?php echo $last; ?><br>
<b>User:</b><?php echo $username; ?><br>
<hr>
<form action="user-permissions.php" method="post">
<?php
while ($stmt->fetch()) {
echo $PagesName;
echo "<br>";
echo "<select name=\"permissionSelect\">";
?>
<option value="<?php echo $view; ?>"><?php echo $view; ?></option>
<option value="No">No</option>
<option value="Yes">Yes</option>
</select>
<br><br>
<?php
echo "<input type=\"hidden\" name=\"PermPagesID\" value=\"".$PermPagesID."\">";
echo "<input type=\"hidden\" name=\"PermUserID\" value=\"".$PermUserID."\">";
}
?>
<?php
echo "<input type=\"submit\" name=\"updatep\">";
echo "<form>";
}
}
?>
<?php
//UPDATE PERSON
require ("cw/connect.php");
if(mysqli_connect_error()){
echo mysqli_connect_error();
exit();
}
if(isset($_POST['updatep']))
{
$view = mysqli_real_escape_string($connect,$_POST['permissionSelect']);
$PermPagesID = mysqli_real_escape_string($connect,$_POST['PermPagesID']);
$PermUserID = mysqli_real_escape_string($connect,$_POST['PermUserID']);
$query = "UPDATE permissions SET view = ? WHERE PermPagesID = $PermPagesID AND PermUserID = $PermUserID";
$stmt = mysqli_prepare($connect, $query);
if($stmt){
mysqli_stmt_bind_param($stmt, "s", $view);
mysqli_stmt_execute($stmt);
mysqli_stmt_fetch($stmt);
echo "Updated to: ".$username;
}else{
echo "object not created";
}
}
?>
您可以將輸入的名稱更改爲數組,例如'name = \「PermUserID [] \」',然後循環並更新 – Hackerman
正如Hackerman所說,輸入中不能有重複名稱,否則會被最後一個覆蓋。所以你把[]添加到名稱的末尾,將其轉換爲一個值數組,然後將其循環。 – LordNeo