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

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -