如何获取要存储的表的ID [英] How to fetch the ID of the table to be stored

查看:51
本文介绍了如何获取要存储的表的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表:

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:

http://www.terminally-incoherent.com/blog/2006/12/04/mysql-how-to-get-the-key-of-last-inerted-row- in-php/

无论您做什么,都将必须进行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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆