简体   繁体   English

PHP Mysql查询插入

[英]PHP Mysql query insert

I have a table - 'A' which has primary key - id which is auto incremented.I also have a table - 'B' which also has the column 'id'. 我有一个表-'A'有主键-id是自动递增的。我还有一个表-'B'也有列'id'。 Now I want to add entries to the table A so that value of id attribute of table 'A' which just got inserted to the table also gets inserted to B while other attributes of B can remain NULL.I know I can just make one table with all the columns but is there a way to do this with two tables?? 现在我想向表A添加条目,以便刚插入表中的表'A'的id属性值也插入到B中,而B的其他属性可以保持为NULL。我知道我只能创建一个表与所有列,但有没有办法用两个表做到这一点?

Use the mysqli_insert_id () function right after you run the insert to table a. 在对表a执行插入操作之后,立即使用mysqli_insert_id()函数。 That will give you the value of the auto incremented id from table a which you can then populate in the surrogate key column of table b. 这将为您提供来自表a的自动递增ID的值,然后您可以将其填充到表b的代理键列中。

Read about it here: http://php.net/manual/en/mysqli.insert-id.php 在这里阅读有关内容: http : //php.net/manual/en/mysqli.insert-id.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM