简体   繁体   English

PHP发布最后一个动态表单

[英]PHP Posting last dynamic Form

I have used a while loop to display Dropdown yes and no which displays a users permissions for each page, so page name and Yes or no drop down for each page. 我使用了while循环来显示Dropdown yes和no,这显示了每个页面的用户权限,因此页面名称以及每个页面的Yes或no下拉列表。

The form appears correct and displaying correct database value for each user as default. 该表单显示正确,并且默认情况下为每个用户显示正确的数据库值。 However if I change one of the page permission from yes to no or vice verser its only updating the last of the dropdowns in the database and not the rest the rest thats changed. 但是,如果我将页面权限之一从“是”更改为“否”,反之亦然,它只会更新数据库中最后一个下拉列表,而不更新其余部分。 Any Ideas what I am missing here? 任何想法,我在这里想念什么?

<?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";

}
}

?>

The reason is because you cannot have the same name used more than once in your form elements. 原因是因为您不能在表单元素中多次使用相同的名称。 Otherwise, only the last one will be used. 否则,将仅使用最后一个。

You will need to give them all a different name, or mimic array syntax as follows. 您将需要给它们全部一个不同的名称,或模拟数组语法,如下所示。 For example, instead of: 例如,代替:

echo '<select name="permissionSelect">';

Use: 采用:

echo '<select name="permissionSelect[]">';

Then in your PHP script you can reference these by looping through them with a foreach: 然后,在您的PHP脚本中,可以通过使用foreach遍历它们来引用它们:

foreach($_POST['permissionSelect'] as $permissionSelect){
    //handle it here
}

You will need to do this for the other name attributes inside the loop as well. 您还需要对循环中的其他名称属性执行此操作。

Lastly, you will notice that I removed you backslashes from your code. 最后,您会注意到我从代码中删除了反斜杠。 This is because you both PHP and HTML can use either single or double quotes. 这是因为PHP和HTML都可以使用单引号或双引号。 And while there is some difference between the two in PHP, in most cases you can just interchange them. 虽然两者在PHP中有所区别,但在大多数情况下,您可以互换它们。 So, if PHP is using single quotes, then use double quotes for your HTML attributes, or vice versa. 因此,如果PHP使用单引号,则对HTML属性使用双引号,反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM