php - Insert newly created id value into the column of another table as Foreign key -


hello php , mysql noob here

i'm creating form create new user , @ same time add contact
user , contact in 2 different table

and need newly user_id reference user_id(fk) in tbl_contact

how it?

first of insert user

mysql_query("insert tbl_user (...) values (...)"); 

then retrieve last inserted id

$liid=mysql_insert_id(); 

then insert contact id

mysql_query("insert tbl_contact (user_id,...) values ($liid,...)"); 

refrence

if want use mysqli

$mysqli->query("insert tbl_user (...) values (...)"); $liid=$mysqli->insert_id; $mysqli->query("insert tbl_contact (user_id,...) values ($liid,...)"); 

Comments

Popular posts from this blog

How to check data type in C++? -

javascript - Is getTimezoneOffset() stable during daylight saving transition? -

formula - R: how to pass in a reference to the variable in glm or lm? -