1

I am trying to send back the last inserted id after I insert some data into my database. I am successfully entering the data into the database, but there is no response data being sent back. I am using Php PDO and it is hosted on Google App Engine and my headers are set to application/json in the dbconnect.php file. What do I need to add to make it successfully send back response data.

<?php
    require('dbconnect.php');
    $statement=$con->prepare('INSERT INTO customers (fname, lname) VALUES (:fname,:lname)');
    $statement->execute(array(':fname' => $_POST['fname'], ':lname' => $_POST['lname'] ));
    $orderId = $statement->lastInsertId();
    $json = $json_encode($orderId);
    echo $json;
    $con = null;
?>

1 Answers1

1

I got Notice: Undefined index: fname and Notice: Undefined index: lname and Fatal error: Call to undefined method PDOStatement::lastInsertId() – Moustache_Me_A_Question 10 mins ago

You have no name attributes for the form elements.

Therefore you need to add them to your elements.

Here is an example form you can base yourself on:

<form action="" method="post">
    First name:
    <input type="text" name="fname">
<br>
    Last name:
    <input type="text" name="lname">
<br>
    <input type="submit" name="submit" value="Submit">
</form>

That is why you're getting undefined index notices.


$json = $json_encode($orderId); should read as $json = json_encode($orderId); - json_encode() shouldn't be a variable but the function itself.

Also:

$orderId = $statement->lastInsertId(); 

to

$orderId = $con->lastInsertId();

which is why you're getting this message:

Fatal error: Call to undefined method PDOStatement::lastInsertId()

  • lastInsertId() is a method of the PDO class, not the PDOStatement class.

which I found in Bill Karwin's answer when Googling the error:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • I am sending a json object from AngularJS ` $http({ method: "POST", url: "http://example.com/postdata", data: $scope.formData })...` . And $scope.formData is `$scope.formData = { fname:$scope.user.firstName, lname:$scope.user.lastName};` – Moustache_Me_A_Question Mar 31 '15 at 16:36
  • 1
    @Moustache_Me_A_Question Instead of `$orderId = $statement->lastInsertId();` try `$orderId = $con->lastInsertId();` – Funk Forty Niner Mar 31 '15 at 16:37
  • Thanks for the help. Once I changed `$json_encode(...)` to `json_encode(..)` it fixed the errors and I changed the header content-type from application/json to the default. Thanks for your help @Fred -ii- – Moustache_Me_A_Question Mar 31 '15 at 16:44
  • and you were right about the `$con->lastInsertId()` . Thanks! – Moustache_Me_A_Question Mar 31 '15 at 16:45
  • Ahhh lordie of course. I should have spotted that lol @Moustache_Me_A_Question good catch on that :) Mind if I add that to my answer for others to see? and you're welcome. – Funk Forty Niner Mar 31 '15 at 16:46
  • @Moustache_Me_A_Question Would you like to mark it as solved? I've made a few edits in the answer for others to see, should they visit they question in the future. – Funk Forty Niner Mar 31 '15 at 16:50
  • I marked it as answered. If I send a json object to my php script, how do I decode it. I know i use `json_decode(...)` but what goes in the `...` ? – Moustache_Me_A_Question Mar 31 '15 at 17:50
  • @Moustache_Me_A_Question To be perfectly honest with you, I only know a wee bit about json. Have a look at http://php.net/manual/en/function.json-decode.php and this Q&A on Stack http://stackoverflow.com/q/19609354/ and this other Q&A on Stack http://stackoverflow.com/q/1270016/ – Funk Forty Niner Mar 31 '15 at 18:02