Insert record into MySQL and get auto_increment back out again in same PHP file

I have a form which adds records to 2 separate tables, the second keeps track of updates that are done to the first.  In the first, the primary key is just an auto_increment integer, but int he second, I need to specify that number so that the update is assocated with the right record in the first table.  So after the record is inserted into the first table, how can I get that auto_increment value back out for that record?  I could just use the highest number in that column, but that wouldn't necessarially be correct.

can anybody help at all?
y2k1981Asked:
Who is Participating?
 
Diablo84Connect With a Mentor Commented:
you need the mysql_insert_id function

http://www.php.net/manual/en/function.mysql-insert-id.php
0
 
ThGCommented:
Anyway using the highest numberi n that column would necessarily be correct, as auto_increment works by taking highest value and incremeting it by 1. It would be an ugly solution :-)
0
 
y2k1981Author Commented:
Anyway using the highest numberi n that column would necessarily be correct, as auto_increment works by taking highest value and incremeting it by 1. It would be an ugly solution :-)


yes I know, that's why I don't want to use it !!!!!

To Diablo88 - that sounds like what I'm after, but how does it know which field to read the "id" from?
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Diablo84Commented:
It gets the ID from the most recently updated auto increment field in the last insert query.

0
 
y2k1981Author Commented:
cool, i'll have a go
0
 
ThGCommented:
> To Diablo88 - that sounds like what I'm after, but how does it know which field to read the "id" from?

there can be only one auto_increment field.
0
 
y2k1981Author Commented:
ThG, thanks for the feedback, but at the time I didn't realise that it took the value from the auto_incremented field.

And thanks to Diablo84 also for providing the solution
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.