0

I have some PHP code to select data from & insert data into a MYSQL database. Here's what I'm trying to do: - receive a barcode serial number from a JSON payload - connect to the MySQL database - check a database table (passes) to see if the barcode has been registered - if so, write this barcode + timestamp into another table (activations). Then return a JSON mssage that it was successful - if not, return a JSON message that the barcode was not yet registered.

The code below works OK when I check a specific barcode for the first time. But after that it doesn't insert a new entry in the activations table. Instead I get the message 'Activation not recorded' ..

Any idea what I'm doing wrong? Thanks if you can help!

Andrew

<?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();

?>
4

2 回答 2

0

我想if ($statement2->rowCount()==0) {这个说法是错误的,请检查。您不能在那里使用“rowCount”,因为它是一个插入查询。

而且我认为,它被插入的唯一问题是没有正确显示消息。

于 2013-01-15T07:06:02.737 回答
0

您应该在页面的调试阶段打印 sql 错误。

我猜,条形码字段是唯一的。

于 2013-01-15T07:09:23.983 回答