Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-10-05
7
Medium Priority
?
4,215 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 500 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

916 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