我有一个数据库(MySQL),其中有一个名为“dbtest”的表,具有以下字段,
id + v + o1 + o2 + o3 + o4 + g
to clarify that:
v: are the questions field
o1: the first answer
o2: the second
o3: third
o4: forth
g: is the right answer, where I put the number of the right answer for example, if I had a question with a correct answer "o3" the value of "g" should be "3"
(我自己这样做,希望会更容易)
我们还没有达到我所知道的程度,问题是我正在基于之前的方案构建一个测验程序,我制作了一个页面,从数据库中调用问题并将它们一个一个地发布在另一个下面,代码如下:
<?php
include "config.inc";
$SQL = "SELECT * FROM test";
$query = mysql_query($SQL) or die();
echo "<form action=grade.php method=POST>";
while ($row = mysql_fetch_assoc($query)) {
echo "<p>$row[id]. ";
echo "$row[v]<br>";
echo "<input type=radio name=answer[$row[id]] value=$row[o1]>$row[o1]</input><br>";
echo "<input type=radio name=answer[$row[id]] value=$row[o2]>$row[o2]</input><br>";
echo "<input type=radio name=answer[$row[id]] value=$row[o3]>$row[o3]</input><br>";
echo "<input type=radio name=answer[$row[id]] value=$row[o4]>$row[o4]</input><br>";
}
echo "<input type=submit name=submit value=Check>";
echo "</form>";
?>
问题是,当我想写“grade.php”页面时,我从边缘掉了下来,我完全迷失了,如果数据库中只有一个问题,我就可以做到,所以不需要进入循环,但由于数据库预计会收到大量问题,我失去了控制。
我正在尝试类似的事情,
<?php
include "config.inc";
$SQL = "SELECT * FROM test";
$query = mysql_query($SQL) or die();
$n = 0;
while ($row = mysql_fetch_assoc($query)) {
$n++;
$ques[$n] = $_POST['answer'];
if ($ques[$n] == $row[g]) {
echo "<font color=green>Correct</font>"; }
else {
echo "<font color=red>Incorrect<br></font>"; }
}
?>
但没有成功,我所需要的就是处理问题并给出正确/错误的答案,对于每个正确答案(+1),每个错误答案(-1),然后我将以数学方式发布总和。
最佳答案
在 HTML 中,您必须更改单选按钮:
- 无需添加
</input>
, - 对于您设置的答案编号的值,
- 为属性添加引号
echo '<input type="radio" name="'.$answer[$row['id']].'" value="1" />'.$row['o1'].'<br>';
echo '<input type="radio" name="'.$answer[$row['id']].'" value="2" />'.$row['o2'].'<br>';
echo '<input type="radio" name="'.$answer[$row['id']].'" value="3" />'.$row['o3'].'<br>';
echo '<input type="radio" name="'.$answer[$row['id']].'" value="4" />'.$row['o4'].'<br>';
在 PHP 中:
- 您创建一些变量来存储好的和坏的答案,
- 您检查
$row['g']
的值是否为等于答案编号
$query = mysql_query("SELECT * FROM test") or die();
$good = 0; // Number of good answers.
$bad = 0; // Number of bad answers.
while ($row = mysql_fetch_assoc($query))
{
if ( $_POST['answer'][$row['id']] == $row['g'] )
{
echo '<span style="color: green;">Correct</span>';
$good++;
}
else
{
echo '<span style="color: red;">Incorrect</span>';
$bad++;
}
}
关于php - 测验 PHP+MySQL 中的 "LOGIC",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24555736/