?
Solved

php mysql_insert_id

Posted on 2011-02-15
4
Medium Priority
?
533 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
[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
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

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

765 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