简体   繁体   English

如何使用 PHP 从带有提交按钮的循环中获取所选数据

[英]How to get the selected data from a loop with submit button using PHP

I have use loop to view all current data userID and name I've limit the information about the user.我使用循环来查看所有当前数据 userID 和 name 我已经限制了有关用户的信息。 And now it has a button in each of them to click to view more information about the user.现在,每个按钮中都有一个按钮,可以单击以查看有关用户的更多信息。 Now I'm Stuck here I don't know how to get the selected data to view it in viewDetails.php.现在我被困在这里我不知道如何获取所选数据以在 viewDetails.php 中查看它。 I want to use form and I don't know what to put inside the form so i will have identifier to query select in viewDetails.php我想使用表单,但我不知道在表单中放什么,所以我将有标识符来查询 viewDetails.php 中的选择

Here are my codes in selected.php这是我在 selected.php 中的代码

<?php
    if($result = $db->query("SELECT userID, name FROM user ")) {     
            while($row = $result->fetch_assoc())    {
                echo '<form method="POST" action="viewDetails.php"> ';

                    echo $row['userID'], '. ';
                    echo $row['name'];
                    echo '<input type="hidden" name="userid" value="'.$row['userID'].'"><br>';
                    echo '<input type="submit" name="submit" value="View More Details"><br>';
                echo '</form>';
            }
        }else{
            die($db->error);
        }
?>

Heres my code to view the selected data 'viewDetails.php'这是我查看所选数据“viewDetails.php”的代码

    <?php
if(isset($_POST['userid'])){ 
$selected = $_POST['userid'];
    if($result = $db->query("SELECT * FROM user WHERE userID=$selected ")) {     
            while($row = $result->fetch_assoc())    {

                echo $row['name'], '&nbsp;-&nbsp;&nbsp; ';
                echo $row['email'], '&nbsp;-&nbsp;&nbsp; ';
                echo $row['address'], '<br>';

            }
        }else{
            die($db->error);
        }
}
?>

Add a hidden field with the users ID:添加一个带有用户 ID 的隐藏字段:

echo '<input type="hidden" name="userid" value="'.$row['userID'].'"><br>';

And then you can retrieve the ID on the PHP page by doing然后您可以通过执行检索 PHP 页面上的 ID

"SELECT * FROM user WHERE id=".$_POST['userid'].""

Thinking further, it could be worth storing all of the users data in hidden fields (but still display the ID and name) and post them to the PHP page.进一步思考,将所有用户数据存储在隐藏字段中(但仍显示 ID 和名称)并将它们发布到 PHP 页面可能是值得的。 It would then prevent you from needing to query your database again, as you could get all of the data from the first query (if you change it to SELECT *):然后它会阻止您再次查询您的数据库,因为您可以从第一个查询中获取所有数据(如果您将其更改为 SELECT *):

echo '<input type="hidden" name="userid" value="'.$row['userID'].'"><br>'
echo '<input type="hidden" name="name" value="'.$row['name'].'"><br>'
echo '<input type="hidden" name="email" value="'.$row['email'].'"><br>'
echo '<input type="hidden" name="address" value="'.$row['address'].'"><br>'

Change the query .. "SELECT * FROM user WHERE userID=$selected"更改查询 .. “SELECT * FROM user WHERE userID=$selected”

<?php


   if($result = $db->query("SELECT userID, name FROM user ")) {     
            while($row = $result->fetch_assoc())    {
                echo '<form method="POST" action="viewDetails.php"> ';

                    echo $row['userID'], '. ';
                    echo $row['name'];
                    echo '<input type="hidden" name="userid" value="'.$row['userID'].'"><br>';
                    echo '<input type="submit" name="submit" value="View More Details"><br>';
                echo '</form>';
            }
        }else{
            die($db->error);
        }
?>
<?php
if(isset($_POST['userid'])){ 
$selected = $_POST['userid'];
    if($result = $db->query("SELECT * FROM user WHERE userID=$selected ")) {   
            while($row = $result->fetch_assoc())    {

                echo $row['name'], '&nbsp;-&nbsp;&nbsp; ';
                echo $row['email'], '&nbsp;-&nbsp;&nbsp; ';
                echo $row['address'], '<br>';

            }
        }else{
            die($db->error);
        }
}
?>

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

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