因此,我的目的是获得选中的单选按钮的值,并将其与我的数据库答案进行比较。但是,当我执行while循环时,$ x ++会从0变为2。 主页和结果页如下。预先感谢您的帮助。
Main.php
<?php
error_reporting(E_ALL ^ E_WARNING);
$con = mysqli_connect("localhost","root","","mcq");
//connection check
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
};
//get questions
$questions = mysqli_query($con, "SELECT `ID`, `Question`, `Answer` FROM `main`");
$answers = mysqli_query($con, "SELECT `ans1`, `ans2`, `ans3`, `ans4` FROM `answerlist`");
echo("<form action='result.php' method='post'>");
if ($questions->num_rows > 0){
// output data of each row
while(($row = $questions->fetch_assoc()) && ($row1 = $answers->fetch_assoc())) {
echo ($row[ID].". ".$row[Question]."<br/>"."<input type='radio' name='id".$row[ID]."' value='".$row1[ans1]."'/>".$row1[ans1]."<br/>"."<input type='radio' name='id".$row[ID]."' value='".$row1[ans2]."'/>".$row1[ans2]."<br/>"."<input type='radio' name='id".$row[ID]."' value='".$row1[ans3]."'/>".$row1[ans3]."<br/>"."<input type='radio' name='id".$row[ID]."' value='".$row1[ans4]."'/>".$row1[ans4]);
}
};
echo("<br/><button type='submit'>SUBMIT</button>");
echo("</form>");
?>
Result.php
<?php
$count = 0;
$con = mysqli_connect("localhost","root","","mcq");
//connection check
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
};
$questions = mysqli_query($con, "SELECT `ID`,`Answer` FROM `main`");
$qcount = count(mysqli_fetch_assoc($questions));
$x = 1;
while($x < $qcount){
$id = "id".$x;
$answerval = $_POST["$id"];
while($row = $questions->fetch_assoc()){
print_r($row);
$ans = $row['Answer'];
if($answerval == $ans){
$count= $count + 1;
echo("TRUE");
};
};
};
echo("<br/>".$count);
?>