How to know the contents of a foreign key to fill in another table?

1

I will give 2 tables of a database: User and Address. where the user has the idEndereco attribute as the foreign key of the address table. The ids are with AUTO INCREMENT, yet, how do I know what the foreign key value of the address is? Will I have to do the insert in address, and then make a select to be able to get its primary key? Is there a more effective method?

    
asked by anonymous 21.05.2016 / 20:23

1 answer

1

By logic you would have to first insert the record into the address table. With the command $mysqli->insert_id you have access to the last id inserted in the last insert you made, if you are using mysqli to connect to the database. If it is PDO, do: PDO::lastInsertId . If I understand correctly, this solves your problem.

    
21.05.2016 / 22:08