我是php的初学者。我正在尝试创建一个Web应用程序,其中用户得分会针对他回答的每个正确答案进行更新。 脚本如下
<?php
$username="surendra";
$useranswer=$_REQUEST['option'];
$qno=$_REQUEST['a'];
$con=mysqli_connect("localhost","root","","gk");
//检查连接
include('connection.php');
$sql1="SELECT * FROM questions WHERE qid=$qno";
$result=mysql_query($sql1);
while ($row = mysql_fetch_array($result))
{
$correctanswer=$row['answer'];
}
include('connection.php');
$sql1="SELECT * FROM contest WHERE username LIKE '".$username."'";
$result=mysql_query($sql1);
while ($row = mysql_fetch_array($result))
{
$points=$row['points'];
}
if ($username=$correctanswer)
{
$con=mysqli_connect("localhost","root","","gk");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"UPDATE contest SET points=$points+5
WHERE username =$username");
mysqli_close($con);
}
?>
更新功能不起作用请帮助我吗?
答案 0 :(得分:1)
尝试将$username
括在单引号中:
mysqli_query($con,"UPDATE contest SET points=$points+5 WHERE username = '$username'");
if ($username==$correctanswer)
和其他人一样。
然后我建议你学习如何绑定params并使用预处理语句。 阅读here
答案 1 :(得分:0)
您错过了=
标志
if ($username=$correctanswer)
--^
尝试:
if ($username == $correctanswer)
此外,还需要引用$username
:
mysqli_query($con,"UPDATE contest SET points = $points + 5 WHERE username ='".$username."'");
答案 2 :(得分:0)
您的代码中有两次include('connection.php');
。
if ($username=$correctanswer)
应为==
。
考虑使用mysqli_ functions或PDO并保护您的代码免受SQL注入。