如何将数据形式存储在我的sql数据库中的最后一个条目作为php / html中的输出

问题描述 投票:0回答:2

我在php中创建了一个表单,输入的答案存储在mySQL数据库中。它是通过在我的表中将id递增1来存储的。在下一页中,我从存储的数据中获取答案并显示它。但这也将向我展示之前输入的答案。如何仅显示上次输入的ID的答案?另外我不想删除mysql数据库中的条目

php html mysql database mysqli
2个回答
0
投票

在表单页面上:

session_start(); // At the top of the page


$last_inserted_id = mysqli_insert_id($db); // Just after the mysql insert query

// Save the id in session so that it is available on the next page
$_SESSION['last_inserted_id'] = $last_inserted_id;

在另一页上

session_start(); // At the top of the page

if(isset($_SESSION['last_inserted_id'])){

    $the_last_inserted_id = $_SESSION['last_inserted_id'];

    // Query now the database

}

0
投票

如何仅显示上次输入的ID的答案?

$server = "localhost";
$user = "username";
$password = "password";
$db = "database";

$connection = new mysqli($server, $user, $password, $db);
$sql= SELECT * FROM table ORDER BY id DESC LIMIT 1;
$data = $conn->query($sql);

if ($data->num_rows > 0) {

    while($row = $data->fetch_assoc()) {
        echo "id: " .$row["id"];
    }
}

“LIMIT 1”意味着你只想从你的表中获得一行并考虑ORDER BY DESC这意味着你选择的最后一个id号将被选中(因为它是以后代顺序,这意味着你将获得最高身份证号码)

我希望我的榜样足够清楚。

© www.soinside.com 2019 - 2024. All rights reserved.