【发布时间】:2015-06-05 01:53:32
【问题描述】:
在我将一些数据插入数据库后,我正试图发回最后插入的 id。我已成功将数据输入数据库,但没有发回响应数据。我正在使用 Php PDO,它托管在 Google App Engine 上,并且我的标头在 dbconnect.php 文件中设置为 application/json。我需要添加什么才能使其成功发回响应数据。
<?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;
?>
【问题讨论】:
-
将错误报告添加到文件顶部,紧跟在打开 PHP 标记之后,例如
<?php error_reporting(E_ALL); ini_set('display_errors', 1);,然后是其余代码,以查看它是否产生任何结果。 -
我得到了
Notice: Undefined index: fname和Notice: Undefined index: lname和Fatal error: Call to undefined method PDOStatement::lastInsertId() -
你去。对于未发布的表单元素,您没有名称属性,并确保您也使用 POST 方法。反过来,更多来自 PDO 的错误。
-
为你的表单元素添加
name="lname"和name="fname",你可能会开始看到magic发生了;-) -
我不确定我是否理解。它成功地将数据插入到数据库中。我也在使用 POST 方法。我需要为名称属性做什么?谢谢
标签: php google-app-engine http pdo