这是REPLACE
语法
$sql = "REPLACE INTO 2_1_journal (Number,RecordDay, RecordMonth) VALUES ";
$insertQuery = array();
$insertData = array();
foreach ($_POST['row_id'] as $i => $row_id) {
$insertQuery[] = '(?, ?, ?)';
$insertData[] = $row_id;
$insertData[] = $_POST['date_day'][$i];
$insertData[] = $_POST['date_month'][$i];
}
if (!empty($insertQuery)) {
$sql .= implode(', ', $insertQuery);
$stmt = $db->prepare($sql);
$stmt->execute($insertData);
}
但是阅读而不是REPLACE
更好地使用ON DUPLICATE KEY UPDATE
试图这样改变
$sql = "INSERT INTO 2_1_journal (Number,RecordDay, RecordMonth) VALUES ON DUPLICATE KEY UPDATE (RecordDay, RecordMonth) ";
$insertQuery = array();
$insertData = array();
foreach ($_POST['row_id'] as $i => $row_id) {
$insertQuery[] = '(?, ?, ?, ?, ?)';
$insertData[] = $row_id;
$insertData[] = $_POST['date_day'][$i];
$insertData[] = $_POST['date_month'][$i];
$insertData[] = $_POST['date_day'][$i];
$insertData[] = $_POST['date_month'][$i];
}
if (!empty($insertQuery)) {
$sql .= implode(', ', $insertQuery);
$stmt = $db->prepare($sql);
$stmt->execute($insertData);
}
但不起作用(既不插入也不更新)
什么是不正确的?
如果投反对票,请在评论中写下原因(为了我避免写导致投反对票的事情)
更新
将代码更改为此
$sql = "INSERT INTO 2_1_journal (Number, RecordDay) VALUES ON DUPLICATE KEY UPDATE RecordDay";
$insertQuery = array();
$insertData = array();
foreach ($_POST['row_id'] as $i => $row_id) {
$insertQuery[] = '(?, ?)';
$insertData[] = $row_id;
$insertData[] = $_POST['date_day'][$i];
}
获取 SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'ON DUPLICATE KEY UPDATE RecordDay('21', ''), ('22', '')' 附近使用正确的语法
改成这个
$sql = "INSERT INTO 2_1_journal (Number, RecordDay) VALUES ON DUPLICATE KEY UPDATE RecordDay=VALUES(Number)";
$insertQuery = array();
$insertData = array();
foreach ($_POST['row_id'] as $i => $row_id) {
$insertQuery[] = '(?, ?)';
$insertData[] = $row_id;
$insertData[] = $_POST['date_day'][$i];
获取 SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'ON DUPLICATE KEY UPDATE RecordDay=VALUES(Number)('21', ''), ('22', '')' 附近使用正确的语法
将代码更改为此
$sql = "INSERT INTO 2_1_journal (Number, RecordDay) VALUES ON DUPLICATE KEY UPDATE RecordMonth=?";
$insertQuery = array();
$insertData = array();
foreach ($_POST['row_id'] as $i => $row_id) {
$insertQuery[] = '(?, ?, ?)';
$insertData[] = $row_id;
$insertData[] = $_POST['date_day'][$i];
$insertData[] = $_POST['date_month'][$i];
获取 SQLSTATE[HY093]: Invalid parameter number: 绑定变量数与标记数不匹配
我可以得出结论,最后一个示例是有效的 SQL 语法。但为什么会出错?编号、记录日、记录月=?还有$insertQuery[] = '(?, ?, ?)';
3 个变量和 3 个标记?还是我错了?
如果$insertQuery[] = '(?, ?)';
相同 SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
请帮忙。我被卡住了……不知道
工作代码!!!
try {
$sql = "INSERT INTO 2_1_journal (Number, RecordDay) VALUES ";
$insertQuery = array();
$insertData = array();
foreach ($_POST['row_id'] as $i => $row_id) {
$insertQuery[] = '(?, ?)';
$insertData[] = $row_id;
$insertData[] = $_POST['date_day'][$i];
}
if (!empty($insertQuery)) {
$sql .= implode(', ', $insertQuery) . 'ON DUPLICATE KEY UPDATE RecordDay = VALUES(RecordDay);';
$stmt = $db->prepare($sql);
$stmt->execute($insertData);
}
}
catch (PDOException $e){
echo "DataBase Error: " .$e->getMessage() .'<br>';
}
catch (Exception $e) {
echo "General Error: ".$e->getMessage() .'<br>';
}
请发表您的意见。并查看位置ON DUPLICATE KEY UPDATE
!!!