我不同意。我的SQL查詢只能通過PhpMyAdmin發送。如果我試圖通過PHP發送這個特定的查詢,我得到一個錯誤。當我將精確的查詢複製到PhpMyAdmin時,它會毫無問題地完成。SQL錯誤只發生在PHP腳本中
INSERT INTO posts (id, content, poster, timestamp, tags) VALUES ('12056242', 'OMG I just got a #toyota', 'Clunker5', '09/12/14 08:43:36', 'toyota');INSERT INTO `tags` (tag, posts) VALUES ('toyota', 1) ON DUPLICATE KEY UPDATE posts=posts+1; UPDATE `tags` SET posts=posts+1 WHERE tag IN ('toyota');
這是與問題相關的
//Ups one post for all tags entered by the user
if(!empty($tags)){
$tags1 = explode(",", $tags);
$tags_submit = join("','", $tags1);
$tags_insert = join("', 1), ('", $tags1);
$sql = "INSERT INTO posts (id, content, poster, timestamp, tags) VALUES ('$d', '$b', '".$_SESSION['username']."', '$c', '$tags');"
. "INSERT INTO `tags` (tag, posts) VALUES ('".$tags_insert."', 1)
ON DUPLICATE KEY UPDATE posts=posts+1;
UPDATE `tags` SET posts=posts+1 WHERE tag IN ('".$tags_submit."');";
$result = mysql_query($sql);
}else{
$sql = "INSERT INTO posts (id, content, poster, timestamp, tags) VALUES ('$d', '$b', '".$_SESSION['username']."', '$c', '$tags');";
$result = mysql_query($sql);
}
$error = mysql_error();
if($result){
echo "1";
}else{
echo error($sql, $error, "Tags: ".$tags, "Post: ".$b, "ID: ".$d);
}
PHP代碼
的錯誤是
SQL Response: 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 'INSERT INTO `tags` (tag, posts) VALUES ('toyota', 1), ('ohmygoodness', 1) ' at line 1.
編輯:現在,我知道,我不能做一個多查詢,怎麼能我做這個查詢?
INSERT INTO `tags` (tag, posts) VALUES ('".$tags_insert."', 1)
ON DUPLICATE KEY UPDATE posts=posts+1;
UPDATE `tags` SET posts=posts+1 WHERE tag IN ('".$tags_submit."');
你會得到什麼錯誤?你在檢查mysql_error()嗎?插入變量後確定查詢是否正確? – 2014-09-13 00:47:01
今天第3個類似的問題。 – 2014-09-13 00:49:08
INSERT INTO tags(tag,posts)VALUES('toyota',1),('ohmygoodness',1)' - 我從來沒有見過像這樣的VALUES條款,那該怎麼辦? – David 2014-09-13 00:56:44