?
Solved

php mysql_insert_id

Posted on 2011-02-15
4
Medium Priority
?
559 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 1000 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 111

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

578 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