1

Im trying to get the last inserted record to be displayed. At the moment i'm trying to echo my binded value for last inserted id.

$stmt= $dbh->prepare("INSERT INTO child (FName, LName, Age, Sex, Allergies) values (:FName, :LName, :Age, :Sex,:Allergies)");
  $stmt->bindValue(':FName', $_POST['FName'], PDO::PARAM_STR);
  $stmt->bindValue(':LName', $_POST['LName'], PDO::PARAM_STR);
  $stmt->bindValue(':Age', $_POST['age'], PDO::PARAM_STR);
  $stmt->bindValue(':Sex', $_POST['sex'], PDO::PARAM_STR);
  $stmt->bindValue(':Allergies', $_POST['allergies'], PDO::PARAM_STR);
  $add1=$stmt->execute();
  $newchild=$dbh->lastInsertid();

The above is just creating the record

$stmt=$dbh->prepare("INSERT INTO pcdetails (childID, parentID) values ( :newchild, :newparent)");
  $stmt->bindValue(':newchild', $newchild, PDO::PARAM_STR);

Is where im binding the new child record to $newchild. Im only using last insert id for a separate table im making that will auto generate once a child and parent record are inserted. Thats all working correctly... Not getting any bugs.

Now to my problem: Im trying to use the same bindValue to echo the past record that was inserted into the database.

All along I've being just using a query that will display all the records:

$sql = "select child.childid, FName, LName, age, sex, allergies from child ORDER BY LName, FName

But I want just to show the last record.

Ive tried the following:

$sql = "select child.childid, FName, LName, age, sex, allergies from child ORDER BY LName, FName where childid = "<?php echo $newchild; ?>"";

But im getting the error "Parse error: syntax error, unexpected '?'". Im not sure why Im getting this error.

Any help would be great.

Thanks.

  • 4
    This is a ***syntax error***. For those and for understanding help for PHP error messages in general, please consult the following reference question first: [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – hakre Apr 02 '14 at 09:06
  • 1
    Use `prepare` and `bind` with the `$newchild` field in the same way as the previous code. – Spudley Apr 02 '14 at 09:06

1 Answers1

0

Remove the PHP tags as you are already within PHP tags. Also your ORDER BY clause needs to come after the WHERE clause

$sql = "select child.childid, FName, LName, age, sex, allergies 
           FROM child 
           WHERE childid = $newchild
           ORDER BY LName, FName";
RMcLeod
  • 2,561
  • 1
  • 22
  • 38
  • this was the first thing i thought of doing but im getting the error "Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where childid = 66' at line 1" from using the above. – user3007858 Apr 02 '14 at 09:18
  • Just noticed your ORDER BY is in the wrong place you need to move it to after the where `FROM child WHERE childid = $newchild ORDER BY LName, FName` – RMcLeod Apr 02 '14 at 09:20
  • Thanks that was it. Stupid mistake on my part. – user3007858 Apr 02 '14 at 09:27