MYSQL更新查询语法问题

时间:2011-04-19 19:11:47

标签: mysql sql syntax-error sql-update

我目前正在尝试更新数据库中的特定记录,但是虽然我已经彻底检查了语法,但Chrome告诉我,我在某处错了。

任何建议都将非常感谢

$title = $_POST["title"];
$alttext = $_POST["alttext"];
$description = $_POST["description"];
$price = $_POST["price"];
$id = $_POST["ID"];
$insertQuery = "UPDATE cmsproducts SET Title = '$title', Alt_Text = '$alttext', Source = '$target_path', Description = '$description', Price = $price WHERE ID = $id";

// Save the form data into the database 
if ($result = $connector->query($insertQuery)){

// It worked, give confirmation
echo '<center><b><span style="color: #FF0000;">Product added to the database</span></b></center><br /><br />';

}else{

// It hasn't worked so stop. Better error handling code would be good here!
echo('<center>Sorry, there was an error saving to the database</center>');
echo "<center><b>File Name:</b> ".$target_path."<br/>";
die(mysql_error());


}

我已经尝试了没有变量的查询来检查它是否存在问题,但它仍然在我身上尖叫错误:

抱歉,保存到数据库时出错 您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便在'test,... = / images / Pictures /附近使用正确的语法,Source =这是测试的测试图像'在第1行

2 个答案:

答案 0 :(得分:3)

始终转义用户输入(mysql_real_escape_string)或使用PDO并分配参数。看来$alttext变量中有引号或其他特殊字符。例如,

$title = mysql_real_escape_string($_POST["title"]);
$alttext = mysql_real_escape_string($_POST["alttext"]);
$description = mysql_real_escape_string($_POST["description"]);
$price = mysql_real_escape_string($_POST["price"]);
$id = mysql_real_escape_string($_POST["ID"]);
$insertQuery = "UPDATE cmsproducts SET Title = '$title', 
   Alt_Text = '$alttext', Source = '$target_path',
   Description = '$description', Price = '$price' WHERE ID = '$id'";

答案 1 :(得分:1)

由于您的列Description必须在其中包含单个引号,因此您似乎无法转义引号。使用mysql_real_escape_string来转义引号。