开发者

MySQL insert to multiple tables (relational)

开发者 https://www.devze.com 2023-01-07 09:36 出处:网络
tbl_product Name | Creator | UID | Salerank tbl_price Supplier | Price | UID I want to insert a product and then insert开发者_开发技巧 multiple prices into a seperate table. How is it possible to e
tbl_product
Name | Creator | UID | Salerank

tbl_price
Supplier | Price | UID

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.

Thanks,

J


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


Use a GUID for the UID, or better, insert your products and the insert the prices using e.g. the name of the product (assuming unique) to look up the relevant product UID.

0

精彩评论

暂无评论...
验证码 换一张
取 消