我正在使用php和mysql作为数据库创建一个考试页面。我通过while循环显示问题和选项。
我面临的问题是从用户那里获取所选的值。
任何帮助将不胜感激!
以下是代码:
<body>
<form name="form1" method="POST" action="">
<input name="submit" type="submit" value="Start Test">
<?php
$username="root";
$password="";
$database="assesment";
mysql_connect("localhost",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
if(isset($_POST['submit']))
{
$query = "SELECT * FROM demo";
$result = mysql_query($query);
$num = mysql_num_rows($result);
mysql_close();
echo "<b><center>Database Output</center></b><br><br>";
$i=0;while ($i < $num)
{
$qno = mysql_result($result,$i,"qno");
$quest = mysql_result($result,$i,"quest");
$a = mysql_result($result,$i,"a");
$b = mysql_result($result,$i,"b");
$c = mysql_result($result,$i,"c");
$d = mysql_result($result,$i,"d");
echo "
<br><br><b> $qno. $quest<br><br>
Options : </b>
<input type='Radio' Name='Answer $qno' value='A' >A.<span><span> $a
<input type='Radio' Name='Answer $qno' value='B' >B.<span><span> $b
<input type='Radio' Name='Answer $qno' value='C' >C.<span><span> $c
<input type='Radio' Name='Answer $qno' value='D' >D.<span><span> $d
<br><br>";
$i++;
}
}
答案 0 :(得分:0)
首先,不推荐使用mysql_*
函数,不应使用它。使用MySQLi或PDO。用MySQLi发布我的答案。
我会改为另一个while
循环:
<body>
<form name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
if(isset($_POST['submit']) && $_POST['submit'] == "Start Test"){
$username="root";
$password="";
$database="assesment";
$sql = mysqli_connect("localhost", $username, $password, $database);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM demo";
$result = mysqli_query($sql, $query);
$num = mysqli_num_rows($result);
echo "<b><center>Database Output</center></b><br><br>";
while ($row = mysqli_fetch_assoc($result)) {
$qno = $row['qno'];
$quest = $row['quest'];
$a = $row['a'];
$b = $row['b'];
$c = $row['c'];
$d = $row['d'];
echo "<br><br><b> $qno. $quest<br><br>\r\n"
echo "Options : </b>\r\n";
echo "<input type='Radio' Name='Answer[$qno]' value='A' >A.<span><span> $a \r\n";
echo "<input type='Radio' Name='Answer[$qno]' value='B' >B.<span><span> $b \r\n";
echo "<input type='Radio' Name='Answer[$qno]' value='C' >C.<span><span> $c \r\n";
echo "<input type='Radio' Name='Answer[$qno]' value='D' >D.<span><span> $d \r\n"
echo "<br><br>";
}
mysqli_free_result($result);
echo "<input name='submit' type='submit' value='See Results'>\r\n";
mysqli_close($sql);
} elseif(isset($_POST['submit']) && $_POST['submit'] == "See Results") {
// Review answers and show the correct ones
foreach($_POST['Answer'] as $answer){
// Iterate over answers...
}
} else {
echo "<input name='submit' type='submit' value='Start Test'>\r\n";
}
?>
</form>
</body>
您可能还想考虑将提交按钮移到最后,但只要所有内容都包含在<form>
标记内,它就应该提交整个表单。
关于答案,这取决于您存储它们的位置。但是如果你把它们放到一个类似的数组中,你可以循环它并比较结果。例如:
} elseif(isset($_POST['submit']) && $_POST['submit'] == "See Results") {
// Review answers and show the correct ones
$userAnswers = $_POST['Answer'];
// Connect to DB and get Answer Key code... put in array called $keyAnswers
$resultAnswers = array();
for($i=0;$i<count($keyAnswers);$i++){
if(isset($userAnswers[$i])){
// Compare if user answered the question
$resultAnswers[$i] = ($userAnswers[$i] == $keyAnswers[$i])?TRUE:FALSE;
} else {
// Answer is wrong if they did not answer it
$resultAnswers[$i] = FALSE;
}
}
foreach($resultAnswers as $k => $v){
if($v){
echo "You answered Question $k correctly.<br />\r\n";
} else {
echo "You answered Question $k incorrectly.<br />\r\n";
}
}
}