0

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');";
3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by 54uydf
2
echo "Last inserted id: ".mysql_insert_id();

This function will return you the last inserted id!

Votes + Comments
helpful post
0

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');";
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.