Solved

php mysql_insert_id

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
The viewer will learn how to count occurrences of each item in an array.
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.

914 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