我想抓插入數據庫最后一筆數據的ID(為主鍵遞增),把這個ID變量傳遞到下一頁面,方便獲取使用,使用了$select="select last_insert_id() ";來獲取,此方法初次使用,導致不是報錯就是抓空值,有請個位大蝦,伸出援助之手代碼片段:<?php@ $db=new mysqli('localhost', 'bm_user', 'password', 'ivortt');if (mysqli_connect_errno()){echo "Connect Error: Could not connect to database. Please try again later.<br />";exit;}//insert values into table joblist$query_info="insert into hr_jl(id,timecoll,college1,school_type,college,craft,degree,english,compute,interest,time_one,time_two,item_name,practice_responsibility0,description_practice0) values('".$id."','".$timecoll."','".$college1."','".$school_type."','".$college."','".$craft."','".$degree."','".$english."','".$compute."','".$interest."','".$time_one."','".$time_two."','".$item_name."','".$practice_responsibility0."','".$description_practice0."')";$result=$db->query($query_info);if($result) {echo include("student_baseinfo.php?id=$id"); }else {echo "Insert Error:An error has occurred. The item was not added.<br />";}$db->close();?>我該怎么使用last_insert_id()獲得ID是值?補充一下: 字段:ID類型:BIGINTAUTO_INCREMENT
2 回答

Smart貓小萌
TA貢獻1911條經驗 獲得超7個贊
if($result) {
// 加這一句
$id = $db->insert_id;
echo include("student_baseinfo.php?id=$id");
}else {
echo "Insert Error:An error has occurred. The item was not added.<br />";
}
再有在插入的時候,你那id值不用管嘛,插完了你只要$db->insert_id一下就知道插入的那條記錄的ID是多少了,你干嘛費力巴撒的在沒插之前就去確定當前要插的記錄的ID呢,ID本來就是自增的,不用管他的嘛
- 2 回答
- 0 關注
- 155 瀏覽
添加回答
舉報
0/150
提交
取消