0

我的 uni 测验有问题,我已经搜索了许多可能的解决方案,但没有一个适合我的需求。我的测验在数据库中运行,我的问题在于结果页面。在测验结束时,最后一个问题被捕获在下一页上,但是没有显示正确答案的总数,它只显示“1/6”。我想也许它显示了错误答案的数量,但我已经测试了这个理论几次,但事实并非如此。

这是我的结果页面的代码:

<?php session_start();
//connect to the database
include('connectDB.php');

//has the user selected a radio button
if (isset($_REQUEST['value']))
{
    {
        $_SESSION['total']+= $_REQUEST['value'];
    }     
}
else
{
    //No value inputted from the user
    echo "Please select an answer";
    $id--;
}

//collecting the answers and tallying the score
$question = mysql_query("SELECT * FROM 'tblquiz';");
$x = 0;
$score = 0;

while($row = mysql_fetch_assoc($question))
{
    echo $row['questions'].'<br/>';

    $answered = $row['answer'.$_POST['value'.$x]];
    $correct = $row['value'];

    if($answered == $correct)
    {
        $score++;
        $acolor = 'green';
    }

    else
    {
        $acolor = 'red';
    }

    echo 'You answered <font color='.$acolor.'>'.$answered.'<font color=black> <br/>';

    echo 'The correct answer was '.$correct.'<br/>';
    echo '----------------------------------- <br/>';

    $x = $x + 1;
}



echo 'Your score is '.$score.'/'.$x;
mysql_close($db);
?>

现在,此代码中的某些变量可能不需要存在,但我一直在尝试不同的解决方案,这是我尝试过的最新解决方案。

任何帮助将不胜感激,如果可能的话尽快!

谢谢你。

更新代码

到目前为止,这是我的代码的更新版本:

<?php session_start();
//connect to the database
include('connectDB.php');

//has the user selected a radio button
if (isset($_REQUEST['value']))
{
    {
        $_SESSION['total']+= $_REQUEST['value'];
    }     
}
else
{
    //No value inputted from the user
    echo "Please select an answer";
    $id--;
}

//collecting the answers and tallying the score
$question = mysql_query("SELECT * FROM tblquiz");
$x = 0;
$score = 0;

echo '<h1>Results</h1>';

var_dump($row);
var_dump($_POST[$row]);
while($row = mysql_fetch_assoc($question))
{
    echo $row['questions'].'<br/>';

    $answered = $_POST['value'.$x];
    $correct = $row['value'.$x];

    if($answered == $correct)
    { //displays the correct answer in green
        $score++;
        $acolor = 'green';
    }

    else
    { //displays the wrong answer in red
        $acolor = 'red';
    }

    //users' chosen answer
    echo 'You answered <font color='.$acolor.'>'.$answered.'<font color=black> <br/>';
    //correct answer
    echo 'The correct answer was '.$correct.'<br/>';
    echo '----------------------------------- <br/>';

    $x = $x + 1;
}


echo '<p><strong>Your score is '.$score.'/'.$x.'</strong>';
mysql_close($db);

?>

var_dump($row);并且var_dump($_POST[$row]);都给出了NULL的输出

4

0 回答 0