SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Apr 2002
    Location
    Miami
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getting LAST_INSERT_ID

    I have a form that when filled out gets inserted into the database and creates the Job Ticket Id. The person filling out the form gets forwarded to the next page where he fills out the work order relating to that job id.

    Since the job id has just been generated I can figure out how to query the database for that particular jobid. I came across LAST_INSERT_ID and I have been trying to use that but I must be doing something wrong.

    Here is the way I am using it.


    PHP Code:
    $getjobmysql_query("SELECT jobid FROM job WHERE jobid= LAST_INSERT_ID()"); 

    This just returns a syntax error. I have tried arranging the statement in various ways but no go. If you have any ideas of how to accomplish this let me know, thanks

    Edit to this post- I added the empth parenthesis at end of LAST _INSERT_ID which got rid of the errors but I still get nothing. I wonder if its not possible to do this once you have changed pages.

    Okay never mind I got it, in case anyone else needs this info here is the code
    PHP Code:
    <?php
        $getjob
    mysql_query("SELECT LAST_INSERT_ID() AS job");
        if(!
    $getjob){
        echo(
    "Error:" mysql_error() . "check your syntax");
        }
        while (
    $row mysql_fetch_array($getjob)) {
        
    $jobid $row["job"];
        }
    ?>
    <?=$jobid?>
    The only problem is you can not reference it from another page, it has to be done at the same time you do your insert.
    Last edited by whiterabbit; May 27, 2003 at 20:05.

  2. #2
    SitePoint Zealot Mr Chocolate's Avatar
    Join Date
    May 2002
    Location
    Australia
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it is possible all you do is put your first insert query on the following page. Then put this under your insert query.

    PHP Code:
    $jobid mysql_insert_id(); 
    then do your select like this

    PHP Code:
    $getjob=mysql_query(" select jobid from job where jobid = '$jobid' "); 
    that should work...

  3. #3
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The syntax of using LAST_INSERT_ID() in the MySQL query is:
    Code:
    SELECT LAST_INSERT_ID() FROM job;
    Alternatively you could do it the way Mr Chocolate suggested using PHP.
    Oh no! the coots are eating my nodes!

  4. #4
    SitePoint Zealot Mr Chocolate's Avatar
    Join Date
    May 2002
    Location
    Australia
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wonder which way is faster? I'm guessing the Mysql way LAST_INSERT_ID() i've never done it that way before. But i think php creates the last id entered any way so calling the funcion mysq_insert_id() would take no time at all any way. oh well at least it works for you.

  5. #5
    SitePoint Addict
    Join Date
    Apr 2002
    Location
    Miami
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I understand right, in either way, you have to have the select query on the same page as the insert query.

    The idea of putting the insert query on the next form page would work. Am I understanding correctly then, you must get this variable at the same time as the insert?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •