Kazi Shahadat Hossain Kazi Shahadat Hossain - 6 months ago 12
MySQL Question

How can i get a auto generated primary key id from a table after completion of insertion query?

I have to insert in two table. but for insertion in second table i need the latest id of the first table after insertion in first table. since second table is to many to many relation of first table and another table.
table1(id, name, age) and table2(id, street, table1_id). 1st i will insert into table1 then the id of the inserted row will insert into table2 as table1_id. how i can get the id of last inserted row of table1.

Answer

You can simple use mysql_insert_id(); just after your insert query like this:

$sql_save12 = mysql_query("insert into table(classId,pay_mode_id,) values ('44','2',)");

$insert = mysql_insert_id();

$insert will be your new inserted id.