我正在尝试检索用mysql_insert_id()插入的最后一个id号,但总是返回0,我的id字段是自动增量所以我不知道为什么它返回0谢谢。请帮忙
include 'C:\xampp\htdocs\Student_evaluation\functions.php';
if(!loggedin())
{
header("Location: http://localhost/dev/userarea.php");
exit();
}
if(isset($_POST['submit']))
{
//get data
$name = $_POST['name'];
$f_lastname = $_POST['f_lastname'];
$second_lastname = $_POST['second_lastname'];
$student_number = $_POST['student_number'];
$semester_year = $_POST['semester_year'];
$course = $_POST['course'];
$section = $_POST['section'];
$grade = $_POST['grade'];
$student_perform = $_POST['student_perform'];
$comment_box = $_POST['comment_box'];
$sql = "INSERT INTO `students`(`name`, `first_lastname`, `second_lastname`, `numero_estudiante`, `semester`, `course`, `section`, `f_grade`, `students_perform`, `comments`)
VALUES ('$name','$f_lastname','$second_lastname','$student_number','$semester_year','$course','$section','$grade','$student_perform','$comment_box')";
$con = mysqli_connect("localhost","root","","rememberme");
$result = mysqli_query($con, $sql);
echo "ID of last inserted record is: " . mysql_insert_id();
}
答案 0 :(得分:0)
你应该做这样的事情(使用mysqli_insert_id
):
$con = mysqli_connect("localhost","root","","rememberme");
$sql = "INSERT INTO ...";
$result = mysqli_query($con, $sql);
echo "ID of last inserted record is: " . mysqli_insert_id($con);
mysql_insert_id和mysqli_insert_id都不同,您使用的是mysqli
,因此请使用mysqli_insert_id
代替mysql_insert_id
,最好使用mysqli而不是mysql。
答案 1 :(得分:0)
您正在使用一个库( mysqli )执行查询,然后使用另一个库( mysql )来获取自动增量ID。那不行。在其他问题中,您甚至无法使用第二个库连接到数据库!
始终使用mysqli或更好的PDO,这将帮助您填补盲目的安全漏洞。