php - Insert Last Primary Key from One Table into Another as Foreign Key -


Finding a way to insert the last primary key from Table1 as a foreign key I'm trying to Table2 . .

So far, I have tried as a Blobblabblabblas from the table 1 with foreign code as SELECT max ('id')

Works if a user registers at that time; However, if 5 users are registered at the same time, then the foreign key is wrong.

You can do this.

Creating a Trigger

You can create a trigger after inserting table1 in table2 . for example;

  Create the trigger 'Table 1` in the second table on INSERT for table' 2 'for each row table 2 ST user_id = NEW.id, name = NEW.username;  

Using PHP

You can do this, for example; (You will bind and sanitize the input, but for illustration, I will not be)

  $ mysql-> Query ("INSERT INTO` table1` SET 'Username =' Poccans'"); $ IntforeignKey = $ mysql- & gt; Insert_id; $ Mysql- & gt; Query ("INSERT INTO` table2` SET` id` = ". $ IntForeignKey);  

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -