<?php
$connection=mysql_connect('localhost','root',"");
if(!$connection)
{
die("database connection failed".mysql_error());
}
$db_select=mysql_select_db("quiz_game",$connection);
if(!$db_select)
{
die("database connection failed".mysql_error());
}
if(!isset($_POST['submit']))
{
echo "<form method=\"post\" action=\"air.php\">";
$sub_result=mysql_query("SELECT * from questions where category='air' order by rand() limit 0,5",$connection);
if(!$sub_result)
{
die("database query failed". mysql_error());
}
while ($sub_row=mysql_fetch_array($sub_result))
{
$id=$sub_row["qno"];
$question=$sub_row["question"];
$option1=$sub_row["option1"];
$option2=$sub_row["option2"];
$option3=$sub_row["option3"];
$option4=$sub_row["option4"];
$answer=$sub_row["answer"];
echo "<h3>Q".$id." :".$question."</br></h3>";
echo"</br>
$option1
<input type= radio name=\"{$id}\" value=\"{$option1}\" >
</br>
$option2
<input type= radio name=\"{$id}\" value=\"{$option2}\">
</br>
$option3
<input type= radio name=\"{$id}\" value=\"{$option3}\">
</br>
$option4
<input type= radio name=\"{$id}\" value=\"{$option4}\">
</br></br>";
}
echo"<input type='submit' value='see how you did it' name='submit'>";
echo"</form>";
}
if(isset($_POST['submit']))
{
$total=0;
$answer=mysql_query("select qno,answer from questions",$connection);
while($ans=mysql_fetch_assoc($answer))
{
if($_POST[$ans['qno']]==$ans['answer'])
{
$total++;
}
else
{
}
}
echo"<p align=center><b>you scored $total</b>";
}
?>
问题是即使我没有选择单个单选按钮它给我$结果为2.当数据库中只有5个问题没有问题但是当我添加150个问题并添加随机函数到查询,它提前创造了问题。提前了
答案 0 :(得分:0)
只是为了调试,您可以验证表单值:
echo "<pre>".print_r($POST)."</pre>";
答案 1 :(得分:0)
在你的检查 -
$_POST[$ans['qno']]==$ans['answer']
如果POST中没有数据且$ ans ['answer'] = 0,则返回true。因此,您需要将此代码更改为
$_POST[$ans['qno']]===$ans['answer']
希望它能够正常工作