![](/img/trans.png)
[英]php retrieving id of the last record inserted and insert it in another table
[英]retrieving last inserted ID of a table for using as Foreign key in another table
我有table(table1和table2),table1的ID是table2的外鍵。 在INSERT中我想保存插入到table1中的最后一個ID,以插入到table2的外鍵中。 在你的想法中,最好的方法是什么?
在您的代碼中使用像這樣:
$result= mysql_query("Your SelectQuery");
$lastId = mysql_insert_id();
並使用$lastId
變量。 你可以得到你的REsult
。
您還可以使用JOIN
對於連接2表並使用last_insert_id
表1。
使用LAST_INSERT_ID()
函數獲取最后插入的自動遞增列的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.