第1行SQL语法错误;但似乎在我的问题中


"SQL syntax error at line 1" but seems to be in my query

我得到这个错误:你有一个错误在你的SQL语法;查看与MySQL服务器版本对应的手册,以便在第1行'(update_id, update_when, update_title, update_text, update_who) VALUES'附近使用正确的语法

我已经搜索了这个网站和许多其他解决方案,我似乎找不到它。我已经尝试了我能找到的每一个建议,现在是时候看看我是否错过了一些明显的东西(在过去的3天里)。请看看我的代码,看看什么是错的。我用随机echo = "1"测试了代码;在我的代码中找到它中断的地方,我将从那里发布。非常感谢提前,如果你需要更多的我张贴,请随时问。

$update = mysql_query("INSERT INTO `cbhof_news` SET (`update_id`, `update_when`, `update_title`, `update_text`, `update_who`) VALUES (NULL, `$when`, `$title`, `$text`, `$who`") or die(mysql_error());
echo "1"; //this one is not showing yet, so I know its borked here.
if (mysql_query($update)){
        //header('location: news.php');
        echo "Posted! <br /><br />$title - by: $who<br />$text";
    }else{
        die('We are sorry, this entry could not be created. <br />Error: ' . mysql_errno() . ': ' . mysql_error());
    }
}

再次感谢! !

编辑:好的,我的代码现在读(感谢Rakesh Sharma):
$update = mysql_query("INSERT INTO cbhof_news (`update_id`, `update_when`, `update_title`, `update_text`, `update_who`) VALUES ('', '$when', '$title', '$text', '$who')") or die(mysql_error());
if ($update == TRUE){
        //header('location: news.php');
        echo "Posted! <br /><br />$title - by: $who<br />$text";
    }else{
        die('We are sorry, this entry could not be created. <br />Error: ' . mysql_errno() . ': ' . mysql_error());
    }
}
?>

但是现在我看到数据库中有多个条目。第一次只插入1,第二次是2,然后是4,然后是8 ....

任何想法吗?

尝试在查询变量上使用单引号并删除SET关键字,同时正确关闭查询

$update = mysql_query("INSERT INTO `cbhof_news` (`update_id`, `update_when`, `update_title`, `update_text`, `update_who`) VALUES ('', '$when', '$title', '$text', '$who')") or die(mysql_error());

查看更多插入查询:- http://dev.mysql.com/doc/refman/5.6/en/insert.html

使用SET,您需要查询:-

INSERT INTO table SET col_name = value,col_name = value,col_name = value,... 

如果要插入新行,请不要在查询中使用SET

$update = mysql_query("INSERT INTO `cbhof_news` (`update_id`, `update_when`, `update_title`, `update_text`, `update_who`) VALUES (NULL, '$when', '$title', '$text', '$who'") or die(mysql_error());

删除SET关键字。

$update = mysql_query("INSERT INTO `cbhof_news` (`update_id`, `update_when`, `update_title`, `update_text`, `update_who`) VALUES (NULL, `$when`, `$title`, `$text`, `$who`") or die(mysql_error());

并确保您的值被正确转义以防止sql注入。

INSERT INTO cbhof_news (update_id, update_when, update_title, update_text, update_who) VALUES (NULL, '$when', '$title', '$text', '$who')")
3的变化:
1_删除设置,因为你是插入而不是更新。
2_用'代替'的值。
在结束时,在括号结束之前关闭"。
找到上面编辑过的代码,当你替换它时,代码应该可以工作。