无法解决此SQL错误

时间:2012-02-24 17:32:13

标签: php mysql

我有一个insert语句,其中收到以下错误消息:

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 'WHERE ref = '6u5i14e'' at line 1

这是我的php文件:

    <?php

$secret_question = mysql_real_escape_string($_REQUEST['secret_question']);
$secret_anwser = mysql_real_escape_string($_REQUEST['secret_anwser']);


$con = mysql_connect("localhost","*****","*****");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db('*****', $con);

$sql="INSERT INTO public (secret_question, secret_anwser) VALUES ('$secret_question','$secret_answer') WHERE ref = '". $_SESSION['ref']."' ";

if (!mysql_query($sql,$con))
  {
        die('Error: ' . mysql_error());
  }
  else 
  {
      echo '<hr><h3>Your Account Has Now Been Activated. <br/> <a href = "votenow.php">Please Login To Vote</a></h3><hr>';

  }
?>

我在这里做错了什么?

4 个答案:

答案 0 :(得分:3)

检查拼写“回答”。您在代码中的不同位置拼写不同。

答案 1 :(得分:2)

您需要使用更新而不是插入。

$sql="UPDATE public SET 
   secret_question='$secret_question', secret_anwser='$secret_answer' 
   WHERE ref = '". $_SESSION['ref']."' ";

答案 2 :(得分:2)

如果要修改现有数据,则应使用UPDATE而不是INSERT。

答案 3 :(得分:1)

INSERT ... VALUES不接受WHERE。如果需要运行WHERE语句,则需要执行以下操作。但是,这似乎应该是更新而不是插入,所以要小心

INSERT INTO public (secret_question, secret_anwser) 
SELECT '$secret_question','$secret_answer'
FROM public
WHERE ref = '". $_SESSION['ref']."' "

建议查询:

UPDATE public
SET secret_question = '$secret_question', 
    secret_anwser = '$secret_answer'
WHERE ref = '". $_SESSION['ref']."' "