php准备好的陈述||语法错误或访问冲突:1064

时间:2014-03-20 09:46:41

标签: php mysql sql pdo prepared-statement

以下代码返回此错误(行号在括号[]中)

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' location, type, capacity, length, qty, serial, cert, lastinsp, inspby, datein, ' at line 1' in D:\Websites\riggingregister\pdo.php:90 Stack trace: #0 D:\Websites\riggingregister\pdo.php(90): PDOStatement->execute(Array) #1 {main} thrown in D:\Websites\riggingregister\pdo.php on line 90

我已经在PHPMyAdmin中手动编辑了该条目以及建议的PHP代码'出来了:

$sql = "UPDATE `riggingregister`.`register` SET `register` = \'100\',
`location` = \'testing\', `type` = \'Chains - 4 leg\', `capacity` = \'10mm\',
`length` = \'testing\', `qty` = \'testing\', `serial` = \'testing\', 
`cert` = \'testing\', `lastinsp` = \'testing\', `inspby` = \'testing\',
`datein` = \'testing\', `dateout` = \'testing\', 
`status` = \'HOLD\', `notes` = \'EDITED!\' WHERE `register`.`id` = 1;";

但我不确定如何将其转化为我准备好的陈述。

我一直在阅读教程,手册以及提供给我的每一个可能的问题,并且无法查看我出错的地方。希望有人能指出我正确的方向。

干杯

echo "the <b>CONFIRM CHANGES</b> button was pressed<br /><br />";

    $query = $dbh->prepare('UPDATE register SET register, location, type, capacity, length, qty, serial, cert, lastinsp, inspby, datein, dateout, status, notes VALUES ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE id = $id');
    $data = array($register, $location, $type, $capacity, $length, $qty, $serial, $cert, $lastinsp, $inspby, $datein, $dateout, $status, $notes);
    $query->execute($data);

    echo "Data has been written to the database!<br /><br />";

2 个答案:

答案 0 :(得分:1)

UPDATE格式错误。您的查询仍然可以通过直接传递$ id来打开。将$ id添加到$ data数组的末尾。

TRY

$query = $dbh->prepare('UPDATE register SET register =?, location =?,... WHERE id = ?');
$data = array($register, $location, $type, $capacity, $length, $qty, $serial, $cert, $lastinsp, $inspby, $datein, $dateout, $status, $notes,$id)
$query->execute($data);

答案 1 :(得分:0)

也许像

echo "the <b>CONFIRM CHANGES</b> button was pressed<br /><br />";

$data = array(
    'register' => $register,
    'location' => $location,
    'type' => $type,
    'capacity' => $capacity,
    'length' => $length,
    'qty' => $qty,
    'serial' => $serial,
    'cert' => $cert,
    'lastinsp' => $lastinsp,
    'inspby' => $inspby,
    'datein' => $datein,
    'dateout' => $dateout,
    'status' => $status,
    'notes' => $notes
);

$fieldDetails = NULL;

foreach($data as $key=> $value) {
    $fieldDetails .= "`$key`=:$key,";
}

$fieldDetails = rtrim($fieldDetails, ',');

$dbh = $this->prepare("UPDATE register SET $fieldDetails WHERE id = :id");

foreach ($data as $key => $value) {
    $dbh->bindValue(":$key", $value);
}

$dbh->bindValue(":id", $id);

if($dbh->execute()) {

     echo "Data has been written to the database!<br /><br />";
}