Question

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.

Était-ce utile?

La solution

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";
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top