Creating two rows at the same time with foreign key relationship

using php and mysql

I have two tables, a users table and a profile table.

When I create a new account on my site, user and profile strings are created at the same time.

I want the foreign key to be referenced from the users table to the profiles table so that the user profile information is stored in a separate table.

My questions are how to ensure the foreign key binds to the correct profiles table.

The current way I would like to do this is to create a new user and profiles row, I would query the profiles in descending order to get the last generated row and grab the profile id and insert it into the user id table profile foreign key. I'm sure there must be a better way to do this, though ... no?

0


a source to share


1 answer


  • Create user with mysql_query () ;
  • Assuming the primary key of the users table is an auto-increment field, call mysql_insert_id () ;
  • Use this as a foreign key when inserting a profile.


+2


a source







All Articles