MySQL insert to multiple tables (relational)

后端 未结 2 1186
一生所求
一生所求 2021-01-17 03:07
tbl_product
Name | Creator | UID | Salerank

tbl_price
Supplier | Price | UID

I want to insert a product and then insert multiple prices into a sep

相关标签:
2条回答
  • 2021-01-17 03:37

    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 讨论(0)
  • 2021-01-17 03:42

    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

    0 讨论(0)
提交回复
热议问题