MySQL UPDATE查询语法错误?

时间:2012-01-29 16:46:33

标签: php mysql syntax mysql-error-1064 sql-update

我对MySQL和PHP比较陌生,而且我一直试图在很长一段时间内更新一个表格,我搜索了Google和SO,我仍然无法理解它。

这是php:

$info = array('about_me' => NULL, 'profile_pic' => NULL, 'political_party' => NULL,         'econ_views' => NULL, 'religious_views' => NULL, 
'abortion_view' =>NULL,'gay_marraige' => NULL, 'other' => NULL);

foreach ($_POST as $key => $value) {
    $info[$key] = mysql_escape_string($value);
}

$about_me = $info['about_me'];
$profile_pic = $info['profile_pic'];
$econ_views = $info['econ_views'];
$religious_views = $info['religious_views'];
$abortion_view = $info['abortion_view'];
$gay_marraige = $info['gay_marraige'];
$other = $info['other'];
$political_party = $info['political_party'];

//Connect to database
require 'db.php';

$query = "UPDATE `users` SET `about_me`=$about_me, `profile_pic`=$profile_pic,   `econ_views`=$econ_views,
       `religious_views`=$religious_views,`abortion_view`=$abortion_view,`gay_marriage`=$gay_marraige, 
    `other`=$other, `political_party`=$political_party WHERE `username`=emoore24";

echo "$query"."<br /><br />";
$result = mysql_query($query) or die(mysql_error());

echo "success"

这是在包含许多文本区域和一个选择元素的表单上运行的。我用简单的字符串作为数据运行所有内容并得到了这个:

  

更新users设置about_me =测试,profile_pic =,econ_views =测试经济,religious_views =测试相对,abortion_view =测试堕胎,gay_marriage =测试gay marraige,other =测试其他,political_party =民主人士WHERE username = emoore24

     

您的SQL语法有错误;查看与您的MySQL&gt;服务器版本相对应的手册,以便在'econ_views = test econ,&gt; religious_views = test rel,abortion_view = test abor'附近使用正确的语法第1行

我假设它很小,但我看不到它。有人可以帮忙吗?

4 个答案:

答案 0 :(得分:4)

您没有在任何字符串文字周围加上引号。

UPDATE `users` SET 
  `about_me`=about_me, 
  `profile_pic`=, 
  `econ_views`=test econ,  
  `religious_views`=test rel,
  `abortion_view`=test abortion,
  `gay_marriage`=test gay marraige, 
  `other`=test other, 
  `political_party`=democrat 
WHERE `username`=emoore24

应该是:

UPDATE `users` SET 
  `about_me`='about_me', 
  `profile_pic`=NULL, 
  `econ_views`='test econ',  
  `religious_views`='test rel',
  `abortion_view`='test abortion',
  `gay_marriage`='test gay marraige', 
  `other`='test other', 
  `political_party`='democrat' 
WHERE `username`='emoore24'

如果您将PDO与准备好的语句一起使用,那么它将更简单,更安全,您不必担心引用或转义文字。例如,以下是我编写该代码的方法:

$info = array(
  'about_me' => NULL, 
  'profile_pic' => NULL, 
  'political_party' => NULL,
  'econ_views' => NULL, 
  'religious_views' => NULL, 
  'abortion_view' => NULL,
  'gay_marriage' => NULL, 
  'other' => NULL
);

$query = "UPDATE `users` SET 
      `about_me`=:about_me, 
      `profile_pic`=:profile_pic, 
      `econ_views`=:econ_views,  
      `religious_views`=:religious_views,
      `abortion_view`=:abortion_view,
      `gay_marriage`=:gay_marriage, 
      `other`=:other, 
      `political_party`=:political_party 
    WHERE `username`=:username";

if (($stmt = $pdo->prepare($query)) == FALSE) {
  $err = $pdo->errorInfo(); die($err[2]);
}

$values = array_intersect_key($_POST, $info);
$values['username'] = 'emoore24';

if ($stmt->execute( $values ) == FALSE) {
  $err = $stmt->errorInfo(); die($err[2]);
}

答案 1 :(得分:2)

您需要引用查询中的文字

UPDATE `users` SET `about_me`='about_me'

对其他领域也这样做。

答案 2 :(得分:1)

您的查询错误。您需要在所有值周围加上引号:

更改您的查询:

$query = "UPDATE `users` SET `about_me`='about_me', `profile_pic`='$profile_pic',   `econ_views`='$econ_views',`religious_views`='$religious_views',`abortion_view`='$abortion_view',`gay_marriage`='$gay_marraige', `other`='$other', `political_party`='$political_party' WHERE `username`='emoore24'";

希望这有效:)

答案 3 :(得分:0)

profile_pic =,看起来也错了。我在mysql IDE或mysql命令行编辑器中手动运行查询以查看问题所在。

我也从一个小的select语句开始并构建它。在我有一个有效的select语句后,我将其切换到更新语句。

相关问题