我知道这可能最终会成为一个错字,但我已经尝试调试我的语法两天了,我已经搜索了很多讨论板并堆叠溢出问题无济于事。
当我尝试执行以下
时 try {
$cnnxn = new PDO("mysql:host=$db_host;dbname=$db_name", $db_username, $db_password);
} catch (PDOException $e) {
die("ERROR: " . $e->getMessage());
}
$query1 = "INSERT INTO reply_statistics (reply_id, assignment_id, cat1, cat2, cat3, cat4, cat5, cat6, cat7, cat8) VALUES (:replyid, :assid, :cat1, :cat2, :cat3, :cat4, :cat5, :cat6, :cat7, :cat8)
ON DUPLICATE KEY
UPDATE reply_statistics SET `cat1`=:cat1, `cat2`=:cat2, `cat3`=:cat3, `cat4`=:cat4, `cat5`=:cat5, `cat6`=:cat6, `cat7`=:cat7, `cat8`=:cat8 WHERE `reply_id`=:replyid";
$query2 = "UPDATE replies SET status=:status, corrected_reply=:correply, score=:score WHERE id=:replyid";
$cnnxn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$cnnxn->exec("SET NAMES utf8");
$query = $cnnxn->prepare($query1);
$query->bindParam(':replyid', $replyid);
$query->bindParam(':assid', $assid);
$query->bindParam(':cat1', $cat1);
$query->bindParam(':cat2', $cat2);
$query->bindParam(':cat3', $cat3);
$query->bindParam(':cat4', $cat4);
$query->bindParam(':cat5', $cat5);
$query->bindParam(':cat6', $cat6);
$query->bindParam(':cat7', $cat7);
$query->bindParam(':cat8', $cat8);
$query->execute();
if ($query !== false)
{
$statusreport = "OK";
}
if($statusreport == "OK"){
$query = $cnnxn->prepare($query2);
$query->bindParam(':replyid', $replyid);
$query->bindParam(':correply', $correply);
$query->bindParam(':status', $status);
$query->bindParam(':score', $score);
$query->execute();
if ($query !== false)
{
echo "<div class=\"alert alert-success\">Tallennettu.</div>";
}
}
$cnnxn = null;
我明白了
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 'SET `cat1`='0', `cat2`='0', `cat3`='0', `cat4`='0', `cat5`='0', `cat6`='0', `cat' at line 3' in /[CENSORED]/do-review.php:56 Stack trace: #0 /[CENSORED]/do-review.php(56): PDOStatement->execute() #1 {main} thrown in /[CENSORED]/do-review.php on line 56
它很好地连接了很好的变量,但它在INSERT -- ON DUPLICATE KEY UPDATE
子句上失败了。
关于表格结构:reply_id
是唯一的,但也有一个单独的id
列,它是自动增量的主键。
答案 0 :(得分:2)
您必须将ON DUPLICATE参数集视为完全单独命名的参数,即使您可以使用相同的变量将数据绑定到它们。而且查询语法有点过时了。
$query1 = "INSERT INTO reply_statistics
(reply_id, assignment_id,
cat1, cat2, cat3, cat4,
cat5, cat6, cat7, cat8)
VALUES (:replyid, :assid,
:cat1, :cat2, :cat3, :cat4,
:cat5, :cat6, :cat7, :cat8)
ON DUPLICATE KEY UPDATE
`cat1`=:cat1a, `cat2`=:cat2a,
`cat3`=:cat3a, `cat4`=:cat4a, `cat5`=:cat5a,
`cat6`=:cat6a, `cat7`=:cat7a, `cat8`=:cat8a ";
$query = $cnnxn->prepare($query1);
$query->bindParam(':replyid', $replyid);
$query->bindParam(':assid', $assid);
$query->bindParam(':cat1', $cat1);
$query->bindParam(':cat2', $cat2);
$query->bindParam(':cat3', $cat3);
$query->bindParam(':cat4', $cat4);
$query->bindParam(':cat5', $cat5);
$query->bindParam(':cat6', $cat6);
$query->bindParam(':cat7', $cat7);
$query->bindParam(':cat8', $cat8);
// parametes for the ON DUP set of params
$query->bindParam(':cat1a', $cat1);
$query->bindParam(':cat2a', $cat2);
$query->bindParam(':cat3a', $cat3);
$query->bindParam(':cat4a', $cat4);
$query->bindParam(':cat5a', $cat5);
$query->bindParam(':cat6a', $cat6);
$query->bindParam(':cat7a', $cat7);
$query->bindParam(':cat8a', $cat8);