我想抓插入數(shù)據(jù)庫(kù)最后一筆數(shù)據(jù)的ID(為主鍵遞增),把這個(gè)ID變量傳遞到下一頁(yè)面,方便獲取使用,使用了$select="select last_insert_id() ";來(lái)獲取,此方法初次使用,導(dǎo)致不是報(bào)錯(cuò)就是抓空值,有請(qǐng)個(gè)位大蝦,伸出援助之手代碼片段:<?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是值?補(bǔ)充一下: 字段:ID類(lèi)型:BIGINTAUTO_INCREMENT
2 回答

Smart貓小萌
TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊
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 />";
}
再有在插入的時(shí)候,你那id值不用管嘛,插完了你只要$db->insert_id一下就知道插入的那條記錄的ID是多少了,你干嘛費(fèi)力巴撒的在沒(méi)插之前就去確定當(dāng)前要插的記錄的ID呢,ID本來(lái)就是自增的,不用管他的嘛

偶然的你
TA貢獻(xiàn)1841條經(jīng)驗(yàn) 獲得超3個(gè)贊
php有個(gè)mysql_insert_id()函數(shù)是取得上一步 INSERT 操作產(chǎn)生的 ID
- 2 回答
- 0 關(guān)注
- 159 瀏覽
添加回答
舉報(bào)
0/150
提交
取消