Solved

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

Posted on 2004-10-05
7
4,209 Views
Last Modified: 2013-12-12
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?
0
Comment
Question by:y2k1981
  • 3
  • 2
  • 2
7 Comments
 
LVL 27

Accepted Solution

by:
Diablo84 earned 125 total points
ID: 12228116
you need the mysql_insert_id function

http://www.php.net/manual/en/function.mysql-insert-id.php
0
 
LVL 14

Expert Comment

by:ThG
ID: 12228195
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
 

Author Comment

by:y2k1981
ID: 12228263
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Expert Comment

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

0
 

Author Comment

by:y2k1981
ID: 12228342
cool, i'll have a go
0
 
LVL 14

Expert Comment

by:ThG
ID: 12228749
> 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
 

Author Comment

by:y2k1981
ID: 12235226
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now