How to know the contents of a foreign key for filling another table?

Asked

Viewed 168 times

1

I will give as an example 2 tables of a database: User and Address. where the user has the idEndereco attribute as a foreign key to the address table. The ids are with AUTO INCREMENT, yet, how do I know what the foreign key value of the address? Will I have to do the Insert at address, and then select to get your primary key? There is a more effective method?

  • I don’t understand very well what you want, could you be more specific? Make a tour also to learn more about the site.

1 answer

1


Logically you would have to enter the record in the address table first. With the command $mysqli->insert_id you have access to the last id inserted in the last Sert you made, if you are using mysqli to make the connection to the bank. If you are PDO, do: PDO::lastInsertId. If I understand correctly, it solves your problem.

  • Thanks, Simon. That’s right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.