无法从 Google App Engine 上的 Php PDO 获得响应 Header
Unable to Get Response Header from Php PDO on Google App Engine
我试图在将一些数据插入数据库后发回最后插入的 ID。我已成功将数据输入数据库,但没有发送回响应数据。我正在使用 Php PDO,它托管在 Google App Engine 上,我的 headers 在 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;
?>
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
您没有表单元素的名称属性。
因此您需要将它们添加到您的元素中。
这是您可以基于的示例表格:
<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>
这就是您收到未定义索引通知的原因。
$json = $json_encode($orderId);
应该读作 $json = json_encode($orderId);
- json_encode()
不应该是变量,而是函数本身。
还有:
$orderId = $statement->lastInsertId();
至
$orderId = $con->lastInsertId();
这就是您收到此消息的原因:
Fatal error: Call to undefined method PDOStatement::lastInsertId()
- lastInsertId() 是 PDO class 的方法,而不是 PDOStatement class。
我在谷歌搜索错误时在 Bill Karwin 的回答中发现:
我试图在将一些数据插入数据库后发回最后插入的 ID。我已成功将数据输入数据库,但没有发送回响应数据。我正在使用 Php PDO,它托管在 Google App Engine 上,我的 headers 在 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;
?>
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
您没有表单元素的名称属性。
因此您需要将它们添加到您的元素中。
这是您可以基于的示例表格:
<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>
这就是您收到未定义索引通知的原因。
$json = $json_encode($orderId);
应该读作 $json = json_encode($orderId);
- json_encode()
不应该是变量,而是函数本身。
还有:
$orderId = $statement->lastInsertId();
至
$orderId = $con->lastInsertId();
这就是您收到此消息的原因:
Fatal error: Call to undefined method PDOStatement::lastInsertId()
- lastInsertId() 是 PDO class 的方法,而不是 PDOStatement class。
我在谷歌搜索错误时在 Bill Karwin 的回答中发现: