使用PHP将数据插入MySQL数据库时出错

使用PHP将数据插入MySQL数据库时出错,php,mysql,Php,Mysql,我得到以下错误 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 '16:45:40, 2012-12-18 16:45:40, Renovated Stone house in Perast, first line, 2012' at line 1 这就是问题所在的SQL查询

我得到以下错误

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 '16:45:40, 2012-12-18 16:45:40, Renovated Stone house in Perast, first line, 2012' at line 1
这就是问题所在的SQL查询

$sql =  "INSERT INTO `wp_posts` (`post_author`, `post_date`, `post_date_gmt`, `post_title`, `post_modified`, `post_modified_gmt`, `xml_id`)";
$sql .= " VALUES ({$postAuthor}, {$postDate}, {$postDate}, {$title}, {$postModified}, {$postModified}, {$xmlId});";

值必须用引号括起来,并且必须确保值中没有引号

最好的方法是使用准备好的语句,例如使用。

此行:

$sql .= " VALUES ({$postAuthor}, {$postDate}, {$postDate}, {$title}, {$postModified}, {$postModified}, {$xmlId});";
应该是:

$sql .= " VALUES ('$postAuthor', '$postDate', '$postDate', '$title', '$postModified', '$postModified', '$xmlId')";
删除包装的:',其中列类型不是varchar

此外,还必须验证是否转义变量中的特殊字符

它应该有用

$sql =  "INSERT INTO `wp_posts` (`post_author`, `post_date`, `post_date_gmt`, `post_title`, `post_modified`, `post_modified_gmt`, `xml_id`)";
$sql .= " VALUES ('$postAuthor', '$postDate', '$postDate', '$title', '$postModified', '$postModified', '$xmlId')";

你需要清理你的输入。不,不应该。或者至少你必须先转义变量。