简体   繁体   English

提交到另一个页面后从数据库获取ID的数据

[英]getting data by id from database after submitting to another page

I m trying to get data on another page by id which is on showdetails.php page.but im unable to please help. 我正在尝试通过id在showdetails.php页面上的另一页面上获取数据。但是我无法帮忙。

Showdetails.php Showdetails.php
this page shows details of user with id and button which will allow user to edit details on another page 此页面显示具有ID和按钮的用户详细信息,允许用户在另一页面上编辑详细信息

<!DOCTYPE html>
<html>
<head>
    <title>Details</title>
</head>
<body>

<?php 
    require('database.php');          
?>

<h1>User Lists</h1>
<?php 
        $select = "SELECT id, firstname, lastname FROM signup";
        $selectdata = $conn->query($select);

        if ($selectdata->num_rows > 0){
            while($row = mysqli_fetch_array($selectdata)) {

            $id = $row['id'];
            $first = $row['firstname'];
            $last = $row['lastname']; 

?>
    <form method="get" action="editdetails.php">
        <p><b>ID: <?php echo $id; ?></b></p>
        <p>Name: <?php echo $first; ?>&nbsp;<?php echo $last; ?></p>
        <?php 
            $edit = "SELECT id FROM signup WHERE id= '" .$id. "'";
            $selectedit = $conn->query($edit);
        ?>
        <p><input type="submit" name="display" value="Edit Details"></p>
    </form>
<?php
        }
    }
?>
</body>
</html>

editdetails.php editdetails.php
On this page, user will be able to edit details, and i want details by id 在此页面上,用户将可以编辑详细信息,而我希望通过ID获得详细信息

<!DOCTYPE html>
<html>
<head>
    <title>Edit User Details</title>
</head>
<body>

<?php 
    require('database.php');

    $select = "SELECT firstname, lastname, age, phone_no, age, username, password FROM signup";
    $selectdata = $conn->query($select);

    if ($selectdata->num_rows > 0){
            $row = mysqli_fetch_array($selectdata); 

            $first = $row['firstname'];
            $last = $row['lastname']; 
            $age = $row['age'];
            $phone_no = $row['phone_no'];
            $username = $row['username'];
            $password = $row['password'];
    }
?>

<?php
    if (isset($_POST['update'])) {
        # code...       
        $first = mysqli_real_escape_string($conn, $_POST['first']);
        $last = mysqli_real_escape_string($conn, $_POST['last']);
        $age = mysqli_real_escape_string($conn, $_POST['age']);
        $phone = mysqli_real_escape_string($conn, $_POST['phone_no']);
        $username = mysqli_real_escape_string($conn, $_POST['user']);
        $password = mysqli_real_escape_string($conn, $_POST['pass']);

        $update = "UPDATE signup SET firstname= '$first', lastname= '$last', age= '$age', phone_no = '$phone', username = '$username', password = '$password' WHERE id= '$id'";
        $updatedata = $conn->query($update);

        if ($updatedata) {
            # code...
            echo $status = "Details Updated";
        }
        else {
            # code...
            echo $status = "Not Updated";
        }
    }

    if (isset($_POST['delete'])) {
        # code...

        $delete = "DELETE FROM signup WHERE firstname = $first";
        $deletedata = $conn->query($delete);

        if ($deletedata) {
            # code...
            echo $status = "Details Deleted";
        }
        else {
            # code...
            echo $status = "Not Deleted";
        }
    }
?>  

    <h1>Edit Details</h1>
    <form method="post" action= "<?php htmlspecialchars($_SERVER['PHP_SELF']) ?>">
    <p>FirstName: <input type="text" name="first" value="<?php echo $first; ?>"></p>
    <p>LastName: <input type="text" name="last" value="<?php echo $last; ?>"></p>
    <p>Phone no: <input type="number" name="phone_no" value="<?php echo $phone_no; ?>"></p>
    <p>Age: <input type="number" name="age" value="<?php echo $age; ?>"></p>
    <p>User: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="user" value="<?php echo $username; ?>"></p>
    <p>Password: <input type="password" name="pass" value="<?php echo $password; ?>"></p>
    <p><input type="submit" name="update" value="Update">
    <input type="submit" name="delete" value="Delete"></p>
    </form>
    <p><?php echo $status; ?></p>


</body>
</html>

Thank You. 谢谢。

Add in your HTML form a hidden input, like so: 在您的HTML表单中添加隐藏的输入,如下所示:

<input type="hidden" name="id" value="<?php echo $id; ?>">

Then in your editdetails.php file you can access it with: $_GET["id"] . 然后,可以在您的editdetails.php文件中使用$_GET["id"]


Update: 更新:

Add the hidden input to the form in Showdetails.php . 将隐藏的输入添加到Showdetails.php中的表单。 Then in editdetails.php add at the top of the page $id = (int)$_GET["id"]; 然后在editdetails.php中 ,在页面顶部添加$id = (int)$_GET["id"];

Then add to your SELECT query in editdetails.php a WHERE statement for selecting the correct user: 然后将WHERE语句添加到editdetails.php中的SELECT查询中,以选择正确的用户:

$select = "SELECT ... FROM signup WHERE id = $id";

For the update query you are then good to go since you are already using there WHERE id = $id . 对于更新查询,那么您就可以开始了,因为您已经在使用WHERE id = $id (but before your $id variable was not defined) (但在未定义$id变量之前)

暂无
暂无

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

相关问题 将数据从div提交到数据库,该数据库在附加id后正在获取数据 - Submitting data from a div to database, which(div) is getting a data after appending a id 提交表单后从另一个页面(如确认页面)重定向后,如何将值保留在表单中 - How to keep the values in a form after getting redirected from another page(like a confirmation page) after submitting the form 从数据库获取数据并显示在另一页上 - getting data from database and displaying on another page Laravel:在提交// 租赁评级功能后从表单中获取另一个 id - Laravel: getting another id from form after submitting// rating functionality for rentals PHP $ _GET值来自另一个页面,但是在提交from之后,我无法将$ _GET [&#39;id&#39;]放在同一页面上 - Php $_GET value is coming from another page, but i cant put that $_GET['id'] on the same page after submitting the from 从数据库获取消息后重定向到另一个页面 - Redirect to another page after getting message from Database 通过将唯一标识从一页转移到另一页来将数据保存在数据库中 - Saving data in database by carrying the unique id from one page to another 提交表单后,它会重定向到操作页面,但不会将数据保存在数据库中 - after submitting the form it redirects to action page but do not save data in database 提交后自动将数据库中的数据重新加载为HTML表单 - Automatically reload data from database into HTML form after submitting 如何从预订页面获取 ID 并在针对该 ID 提交表单后将其显示给管理员? - How to Get an ID from a booking page and show it into admin after submitting form against that ID?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM