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,...)");
if want use mysqli
$mysqli->query("insert tbl_user (...) values (...)"); $liid=$mysqli->insert_id; $mysqli->query("insert tbl_contact (user_id,...) values ($liid,...)");
Comments
Post a Comment