且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MySQL插入到多个表(关系)

更新时间:2023-01-19 13:48:23

在产品表上为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