MySQL查询更新错误,语法?

时间:2012-12-20 08:52:47

标签: php mysql sql

得到类似的查询:

 UPDATE trails SET route = '$route', distance = '$distance', desc = '$description' WHERE route='$route'

它正在返回此错误:

 Error: 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 'desc = 'Bla bla bla' WHERE route='London to Dublin'' at line 1

谢谢!

4 个答案:

答案 0 :(得分:3)

DESC是保留关键字。用后面的滴答声引用它:

UPDATE trails SET route = '...', distance = '...', `desc` = '...' WHERE route = '...'

顺便说一句,您的代码中存在更严重的问题。例如SQL注入。

答案 1 :(得分:2)

[这不是正确答案。]您应该在将字符串传递给查询之前将其转义。

请参阅此处:http://php.net/manual/en/mysqli.real-escape-string.php或此处,如果您使用旧的弃用功能http://php.net/manual/en/function.mysql-real-escape-string.php

答案 2 :(得分:1)

首先:

像那样逃避你的变量

  $route = mysqli_real_escape_string ($route ) ; // if you are using mysqli
  $route = mysql_real_escape_string ($route ) ; // if you are using mysql

 and so on .. with other variables

试试这个:

   UPDATE trails SET route = '".$route."', distance = '".$distance."', `desc` = '".$description."' WHERE route='".$route."'

obs:desc是mysql的保留关键字,所以请使用其他单词或为其做反对。

答案 3 :(得分:0)

试试这个

mysql_query("UPDATE trails SET `route` = '".mysql_real_escape_string($route)."', `distance` = '".mysql_real_escape_string($distance)."', `desc` = '".mysql_real_escape_string($description)."' WHERE route='".mysql_real_escape_string($route)."'");