Solved

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

Posted on 2004-10-05
7
4,208 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

13 Experts available now in Live!

Get 1:1 Help Now