How to fetch the ID of the table to be stored
I have two tables:
restaurant_orders with fields order_id, restaurant_id, tot_amt where order_id is (Autoin开发者_运维技巧c, primary)
order_menus with fields order_id, menu_id, quantity
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.
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:
http://www.terminally-incoherent.com/blog/2006/12/04/mysql-how-to-get-the-key-of-last-inerted-row-in-php/
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())
or with php:
$last_inserted_row = mysql_insert_id($dblink)
精彩评论