Solved

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

Posted on 2004-10-05
7
4,213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.

622 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