i have two from 1]vendor and 2]vendor address detail a vendor can have one or more address detail so what i have done is i have made two different tables for vendor and vendor address with vendor_id(autoincrement) as primary key and foreign key. what i want is when data is inserted in vendor table i want that auto generated primary key to be inserted as foreign key in address table i am using one logic where after inserting into vendor table i sort it desecnding order and extract last inserted record and use that primary key but this would fails if two users are performing this operation simultanously. can anyone suggest me some better logic
You can use mysql_insert_id to retrieve that certain ID: <?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } mysql_select_db('mydb'); mysql_query("INSERT INTO mytable (product) values ('kossu')"); printf("Last inserted record has id %d\n", mysql_insert_id()); ?> PHP: