I have 3 tables in DB, m..m relation
table1:
id1, a,b,c...

table2:
id2, x, y ..

table3:
id1, id2

I'm inserting new rows to table1 using a form, and the P. key field is set to AUTO_INCR, because there's no other property that can be meaningful enough to distinguish between the records..

so I want to be able to insert a new row to table1 and after that insert a new row to table3, while table3.id1 is the table1.id1 that I just added. but here I'm stuck since I have no idea what value was set to the table1.id1! I'm thinking of checking for the max(table1.id1) after the first query is executed..is there a better way to find out what was the value inserted?

here's my code:

$query = "INSERT INTO tbl1 (Id1, a, b, c) VALUE(NULL, '$a', '$b', '$c');";
$query2 = "INSERT INTO tbl3 (Id1, Id2) VALUES ('[B]??[/B]', '$x');";
echo "Last inserted id: ".mysql_insert_id();

This function will return you the last inserted id!

commented: helpful post +6

like this?

$query = "INSERT INTO tbl1 (Id1, a, b, c) VALUE(NULL, '$a', '$b', '$c');";
if (mysql_query($query,$link) ){
  $temp=mysql_insert_id();
  $query2 = "INSERT INTO tbl3 (Id1, Id2) VALUES ('$temp', '$x');";
}

Yes

thank you so much!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.