Php 无法解决此SQL错误
我有一个insert语句,在该语句中我收到以下错误消息:Php 无法解决此SQL错误,php,mysql,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 = m
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>';
}
?>
检查“答案”的拼写。您在代码的不同位置拼写不同。您需要使用更新而不是插入
$sql="UPDATE public SET
secret_question='$secret_question', secret_anwser='$secret_answer'
WHERE ref = '". $_SESSION['ref']."' ";
如果要修改现有数据,应使用“更新”而不是“插入”。INSERT…值不带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']."' "
插入…哪里不是有效的SQL语法,是吗?:)顺便说一下,您在开始时使用$secret\u anwser
,然后在查询中使用$secret\u answer
。请确保您还使用了mysql\u real\u escape\u string()
会话值。将值存储在会话中不会像INSERT….的作业那样逃逸。。。。在重复密钥更新时…
…请不要忘记接受相应的响应。