Solved

php mysql_insert_id

Posted on 2011-02-15
4
510 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 109

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
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.

832 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