我正在创建一个动态测验页面,每个问题有两个选项和一个单选按钮,用于将其值与数据库中的值进行比较,以便知道答案是否正确
问题和选项存储在数据库中,并在代码中检索,每个问题都在自己的表中
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form method="post" class="RegularFont" action="quiz.php">
<?php
require_once 'database.php';
$res = mysql_query("select * from quiz");
while ($row1 = mysql_fetch_array($res)) {''create a table for every entry
echo <<<_END
<div style="padding-bottom: 30px">
<table border="">
<th colspan="2">question 1</th>
<tr>
<td colspan="2"> $row1[0] </td>
</tr>
<tr>
<td>$row1[1]<input type="radio" name='true1[$row1[1]]' value='1'></td>
<td>$row1[2]<input type="radio" name='true1[$row1[1]]' value='2'></td>
</tr>
</table>
</div>
_END;
}
?>
<input type="submit" value="submit">
</form>
</body>
所以每个单选按钮的值可以是1或2,我想将它与数据库中的值进行比较,其中我有正确答案为数字1或2
我试图使用
foreach ($_POST['true1'] as $value)
,但不能得到正确的,我想这是动态工作的任何数量的输入
您可能应该使用:
foreach ($_POST['true1'] as $key => $value)
您需要$key
,这样您就知道该单选项指向数据库的哪一行。