【发布时间】:2013-01-15 07:01:52
【问题描述】:
我有一些 PHP 代码可以从中选择数据并将数据插入 MYSQL 数据库。 这是我正在尝试做的事情: - 从 JSON 有效负载接收条形码序列号 - 连接到 MySQL 数据库 - 检查数据库表(通过)以查看条形码是否已注册 - 如果是这样,将此条形码 + 时间戳写入另一个表(激活)。然后返回一个成功的 JSON 消息 - 如果没有,则返回条码尚未注册的 JSON 消息。
当我第一次检查特定条形码时,下面的代码可以正常工作。但之后它不会在激活表中插入新条目。相反,我收到消息“未记录激活”..
知道我做错了什么吗? 如果您能提供帮助,谢谢!
安德鲁
<?php
//Messages will be returned as JSON data
header("Content-type: application/json");
//read the POST payload
$payload = json_decode(file_get_contents('php://input'), true);
//get a database connection
require_once("../class/Database.php");
$db = Database::get();
//check for a valid pass
$barcode = $payload['barcode'];
$statement = $db->prepare("SELECT * FROM passes WHERE barcode = ?");
$statement->execute(array($barcode));
$row = $statement->fetch(PDO::FETCH_ASSOC);
if ($row) {
$statement2 = $db->prepare("INSERT INTO activations(activationDatetime, barcode) VALUES (?,?)");
$statement2->execute(array(date("Y-m-d G:i:s"),$barcode));
if ($statement2->rowCount()==0) {
print json_encode(array("msg"=>"Activation not recorded"));
} else {
print json_encode(array("msg"=>"Pass successfully activated"));
}
} else
{
print json_encode(array("msg"=>"Pass not registered"));
exit();
}
flush();
exit();
?>
【问题讨论】:
标签: php mysql prepared-statement json insert-into