如何获取要存储的表的ID [英] How to fetch the ID of the table to be stored
问题描述
我有两个表:
restaurant_orders with fields order_id, restaurant_id, tot_amt where order_id is (Autoinc, primary)
order_menus with fields order_id, menu_id, quantity
当用户单击提交按钮时,记录将存储在restaurant_orders
中,并且自动生成字段order_id
.一条记录也将存储在order_menus
表中.两者都将同时发生.
When the user clicks submit button, the record will be stored at restaurant_orders
with the field order_id
being automatically generated. Also a record will be stored at order_menus
table. Both will happen at same time.
我想要的是存储在两个表中的order_id
应该相同.怎么可能?
What I want is the order_id
stored in both tables should be the same. How is it possible?
推荐答案
这是一篇不错的文章,描述了如何做自己想做的事:
Here's a nice article that describes how to do what you want:
无论您做什么,都将必须进行2次INSERT操作,因为这就是插入的工作方式.从那篇文章中,您可以做到纯mySQL:
You're going to have to make 2 INSERTs regardless of what you do because that's just how inserting works. From that article you can do it pure mySQL:
INSERT INTO tab1 (name) VALUES ('some value')
INSERT INTO tab2 (value, tab1_id) VALUES ('another value', LAST_INSERT_ID())
或使用php:
$last_inserted_row = mysql_insert_id($dblink)
这篇关于如何获取要存储的表的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!