Solved

php mysql_insert_id

Posted on 2011-02-15
4
501 Views
Last Modified: 2012-06-21
hi guys,

i've got a file which is my main file for adding orders and i need a bit of help.

the first page of my add orders is to select the customer and add additional delivery details if necessary.

when i started building all of this, i skipped the "add delivery details" aspect because it was causing a headache.  i now think i have an idea of how to do it, but am i little bit stuck.

as the first page starts of the order, it inserts the customers id number, the salesman id, and the status to 1 in a new row in the _orders table.  i also at this stage had the mysql_insert_id put into a session, so that each page thereafter could add more details to the record.

the way i was thinking the delivery details bit would work was it adds any extra details to a new column, and again using a mysql_insert_id, get the id number from that record, and insert it into the _orders table.

so basically, how do i use mysql_insert_query for different queries? i

hope that makes sense

thanks
jack
0
Comment
Question by:jack-lindsay
4 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 250 total points
ID: 34898901
In a nutshell, immediately after running an INSERT query, pull the id with mysql_insert_id

mysql_query("INSERT INTO aTable........etc ");
$id = mysql_insert_id();

If you run another query then do it again, but store the result in a different variable

mysql_query("INSERT INTO aDifferentTable........etc ");
$otherId = mysql_insert_id();
0
 
LVL 4

Expert Comment

by:JayDiablo
ID: 34898907
mysql_insert_id returns the id of the last inserted row for the current connection (in PHP that would be the current script run).

To get different insert ids, you need to call mysql_insert_id after each INSERT query that you perform with mysql_query.

For example:

<?php

$sql = "INSERT INTO table1 VALUES (NULL, 'a', 'b', 'c')";
mysql_query($sql);
$table1_insertId = mysql_insert_id();

$sql = "INSERT INTO table2 VALUES (NULL, 'a', 'b', 'c')";
mysql_query($sql);
$table2_insertId = mysql_insert_id();

Open in new window


Does that help answer the question?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34898912
No, it does not really make sense, but I'll try to offer some explanation.

When you insert a row into a data base table, you can get the auto_increment id number of that row from something like what is shown in the code snippet.

If you want to use information in that row in subsequent queries you can SELECT or UPDATE (or DELETE) using the id number in the WHERE clause with LIMIT 1.

Does that help to clarify how it works?  Man page here:
http://php.net/manual/en/function.mysql-insert-id.php
// MAKING AN INSERT QUERY AND TESTING THE RESULTS
$sql = "INSERT INTO my_table (username) VALUES ('$safe_username')";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
}
// GET THE AUTO_INCREMENT ID OF THE RECORD JUST INSERTED - PER THE DB CONNECTION
// MAN PAGE: http://php.net/manual/en/function.mysql-insert-id.php
$id  = mysql_insert_id($db_connection);

Open in new window

0
 

Author Closing Comment

by:jack-lindsay
ID: 34904517
cant believe i didn't think of that...works a treat :D
thanks for the quick reply
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

705 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

19 Experts available now in Live!

Get 1:1 Help Now