Solved

lastInsertId() variable in PHP not returning records when used in a select statement

Posted on 2013-11-15
10
433 Views
Last Modified: 2013-11-15
Hi all.

I have the following code that is supposed to return some records based on lastInsertId() that is stored as a variable. Unfortunately, it's not returning any records when used in the select statements.

ShortageID is an INT data type and the select statement works when I remove the WHERE clause in the select statement. It returns all records, when I just want the record whose ShortageID = $ID.

What's wrong with my syntax?

Thank you in advance.

  $query = " 
            INSERT INTO ShortageDataEntry ( 
				ShortageProductID,
				Quantity,
	                UserID,
                CreateDate
            ) VALUES ( 

                :ShortageProduct, 
                :Quantity,
                :userid,
                :currentdatetime
            ) 
        "; 
                
        // Here we prepare our tokens for insertion into the SQL query.  
        $query_params = array( 
          
            ':ShortageProduct' => $_POST['ShortageProduct'], 
            ':Quantity' => $_POST['Quantity'], 
            ':userid' => $_SESSION['user']['userid'] ,
            ':currentdatetime' => $fdate
        ); 

$stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
            $ID=$db->lastInsertId(); 

 $smt = $db->prepare("SELECT ShortageProduct FROM ShortageDataEntry INNER JOIN ShortageProducts ON ShortageDataEntry.ShortageProductID = ShortageProducts.ShortageProductID WHERE ShortageDataEntry.ShortageID = '$ID'");


 $smt->bindColumn('ShortageProduct', $shortageproduct);
 $smt->fetch(PDO::FETCH_BOUND);

Open in new window

0
Comment
Question by:Sim1980
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39651816
It may not be working because you are not using an 'autoincrement key field'.  That is what it is supposed to return.
http://php.net/manual/en/pdo.lastinsertid.php
0
 

Author Comment

by:Sim1980
ID: 39651837
I am using an autoincrement field.

 CREATE  TABLE  `myDB`.`ShortageDataEntry ` (  `ShortageID` int( 11  )  NOT  NULL  auto_increment ,
 `ShortageProductID` int( 11  )  NOT  NULL ,
 `Quantity` decimal( 11, 2  )  NOT  NULL ,
  `UserID` int( 11  )  NOT  NULL ,
 `CreateDate` datetime NOT  NULL ,
 PRIMARY  KEY (  `ShortageID`  )  ) ENGINE  =  MyISAM  DEFAULT CHARSET  = utf8;
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39651906
What data base are you using?  What is the value that is returned in $ID?  How do you know if $stmt->execute() worked?
0
 

Author Comment

by:Sim1980
ID: 39651915
It's a MySQL database.

And I know it works because it inserts the new record in the table.
0
 
LVL 2

Accepted Solution

by:
loki0609 earned 500 total points
ID: 39651931
what driver for pdo are you using?

"Returns the ID of the last inserted row, or the last value from a sequence object, depending on the underlying driver. For example, PDO_PGSQL requires you to specify the name of a sequence object for the name parameter."
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39652000
know it works because it inserts the new record in the table.
Wrong answer!  You know it worked, but there is a return value from the method and the script should be testing that return value.  You should also visualize the $ID value.

This article shows how I've mapped the MySQL functions to PDO.  I have found no trouble getting the id in these examples.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 

Author Closing Comment

by:Sim1980
ID: 39652023
Thank you loki.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39652048
What was the solution?  The accepted answer is a cut-and-paste from the online man page for pdo::lastInsertId() and DaveBaldwin posted that link in the first response.
0
 
LVL 2

Expert Comment

by:loki0609
ID: 39652055
hey... i want you to know I have MAD skillzzz when coming to cut and paste:)

that being said sometimes a cut and paste saves you from typing the exact same thing as what was already said...
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39652262
@loki0609: Me, too (sometimes I even read the fine manuals).  But I just want the author of the question to tell us what fixed his issue.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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…
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.

705 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