[英]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.