gpt4 book ai didi

php - 如何在php中显示mysql的自增数据

转载 作者:行者123 更新时间:2023-11-29 10:45:03 27 4
gpt4 key购买 nike

在我的大学数据库项目中,我的 SQL 中有一个自动增量字段卷号。我想要的是,当新入学并插入学生记录时,它会在同一页面上显示所有发现,包括卷号。然而,尽管我尽了最大努力,它的卷数仍然返回 0。

这是代码:

<?php

$con = mysqli_connect("localhost", "root", "") or die("conection error");
mysqli_select_db($con, "hamdard university") or die("dbase error");

if (isset($_POST['subbtn'])) {

$r = "SELECT RollNo FROM admission_form";
$result = mysqli_query($con, $r);
if (mysqli_query($con, $r)) {
$last_id = mysqli_insert_id($con);
}


$n = $_POST['txtname'];
$f = $_POST['txtfac'];
$s = $_POST['txtsem'];

$sql = "insert into admission_form(name,faculty,semester)values ('$n','$f','$s')";
mysqli_query($con, $sql);



echo "<table border=1>
<th>RollNo</th>
<th>Name</th>
<th>Faculty</th>
<th>Semester</th>";



echo "<tr>";

echo "<td>";
echo $last_id;
echo "</td>";

echo "<td>";
echo $n;
echo "</td>";

echo "<td>";
echo $f;
echo "</td>";

echo "<td>";
echo $s;
echo "</td>";

echo "<br>";


}
?>

<html>

<head></head>

<body>
<form name="f1" action="" method="POST">
RollNo:
<input type="text" name="txtroll" readonly> Name:
<input type="text" name="txtname"> Faculty:
<input type="text" name="txtfac"> Semester:
<input type="text" name="txtsem">
<input type="submit" value="done" name="subbtn">
</form>
</body>

</html>

最佳答案

INSERT查询后需要获取$last_id

$sql="insert into admission_form(name,faculty,semester)values ...
mysqli_query($con,$sql);
$last_id = mysqli_insert_id($con);

关于php - 如何在php中显示mysql的自增数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44744261/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com