问题描述
tbl_product
Name | Creator | UID | Salerank
tbl_price
Supplier | Price | UID
我想插入一个产品,然后在一个单独的表中插入多个价格.怎样才能确保两个表都具有相同的UID(理想情况下是一个自动递增字段)?我将与MySQL一起使用PHP.
I want to insert a product and then insert multiple prices into a seperate table. How is it possible to ensure that both tables had the same UID ideally an auto increment field? I will be using PHP alongside MySQL.
谢谢
J
推荐答案
在产品表上为UID提供auto_increment
主键,而在价格表上仅提供常规主键(不能使用auto_increment).插入itnto产品后,使用PHP命令mysql_insert_id()
.这将获取从上一个查询生成的ID,这将是您在products表上生成的UID.给它分配一个变量,然后在价格表的插入语句中使用它.
Make UID an auto_increment
primary key on the products table, but just a regular primary key on the prices table (no auto_increment). After you insert itnto products, use the PHP command mysql_insert_id()
. This will get the ID generated from the last query, which will be your UID generated on the products table. Assign it a variable and use it in your insert statement on the prices table.
http://php.net/manual/en/function. mysql-insert-id.php
这篇关于MySQL插入到多个表(关系)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!