我使用了while循环显示Dropdown yes和no,显示每个页面的用户权限,因此每个页面的页面名称和是或否下拉。
表单显示正确,默认情况下为每个用户显示正确的数据库值。但是,如果我将其中一个页面权限从“是”更改为“否”或“反之亦然”,则仅更新数据库中的最后一个下拉列表而不更改其余的更改。我在这里缺少什么想法?
<?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";
}
}
?>
答案 0 :(得分:0)
原因是您不能在表单元素中多次使用相同的名称。否则,只会使用最后一个。
您需要为它们提供所有不同的名称,或模仿数组语法,如下所示。例如,而不是:
echo '<select name="permissionSelect">';
使用:
echo '<select name="permissionSelect[]">';
然后在PHP脚本中,您可以通过使用foreach循环遍历它们来引用它们:
foreach($_POST['permissionSelect'] as $permissionSelect){
//handle it here
}
您还需要为循环内的其他名称属性执行此操作。
最后,您会注意到我从您的代码中删除了反斜杠。这是因为PHP和HTML都可以使用单引号或双引号。虽然PHP中的两者之间存在一些差异,但在大多数情况下,您可以只交换它们。因此,如果PHP使用单引号,则对HTML属性使用双引号,反之亦然。