php / mysql如何计算在线测验的结果

时间:2013-07-31 03:29:56

标签: php mysql

我正在进行这个项目,项目是从mysql数据库中随机提问。我创建了两个mysql表,一个用于问题,另一个用于答案。然后我使用单选按钮让用户选择正确的选项,问题的代码在下面

<html>
<title>Quiz</title>
<body bgcolor="#666666"> </body>
<h1><center> Quiz </center></h1>
<?php
echo "Welcome Student";
echo
"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<a href=logout.php>Logout</a>";
?>
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$dataselect = mysql_select_db("quiz",$con);
if(!$dataselect)
{
die("Database namelist not
selected".mysql_error());
}
?>
$query = "SELECT * FROM question ORDER
BY RAND() LIMIT 0,1";
$result = mysql_query($query) or die
("Error processing query. ".mysql_error());
echo "<form method = 'post' action =
'submit.php'>";
echo "<table border = '1'>";
while($data = mysql_fetch_array($result))
{
        echo "".$data['Question']."<br>";
        echo "<input type = 'radio'
name='radio' value='Option1'>";
        echo "".$data['Option1']."<br>";
        echo "<input type = 'radio'
name='radio' value='Option2'>";
        echo "".$data['Option2']."<br>";
        echo "<input type = 'radio'
name='radio' value='Option3'>";
        echo "".$data['Option3']."<br>";
}
echo "<tr><td align = 'center'><input type ='submit' name = 'go' value =go>"
echo "</tr>";
echo "</table>"
echo "</form>";

在上面的代码用户被问到一个 随机选择的问题 DB&amp;比这个代码存储他的答案 进入DB

<?php
$connection = mysql_pconnect("localhost", "root", "") or die ("Error connection.".mysql_error());
mysql_select_db("quiz", $connection) or die("Error selecting db. ".mysql_error());
$Option['Option1'] = false;
$Option['Option2'] = false;
$Option['Option3'] = false;
$Option[$_POST['radio']] = true;
 $order= "INSERT INTO radio (Option1,Option2,Option3) VALUES ('".$Option['Option1']."','".$Option['Option2']."','".$Option['Option3']."')";
  $result=mysql_query($order) or die(mysql_error());
echo "<button onClick="window.location.href='studresult.html'"><center><h3>NEXT</h3></center></button>";

?>

现在我要问的是,当用户回答问题时,如何向他们显示正确和错误的答案,并以百分比计算他们的结果。提前谢谢。

0 个答案:

没有答案