Welcome Guest, Not a member yet? Register   Sign In
Correct method of inserting into 2 or more tables
#1

I have 2 tables users and users_address, 'users' primary key, user_id is autoincremented and is a foreign key in the user_address table.
What is best practice for inserting into both tables?
Reply
#2

Use transactions in which :

1- Begin transaction
2- insert the user with null address, get its insert(ed)ID
3- insert the address with user_id (from above), get the insert(ed)ID of the address
4- Update user record, setting address_id to the obtained insert(ed)ID from above.
5- Commit Transaction
Reply
#3

(08-06-2021, 01:54 AM)gosocial2 Wrote: Use transactions in which :

1- Begin transaction
2- insert the user with null address, get its insert(ed)ID
3- insert the address with user_id (from above), get the insert(ed)ID of the address
4- Update user record, setting address_id to the obtained insert(ed)ID from above.
5- Commit Transaction

can you make a little example please.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB