使用PHP& MYSQL数据库,我怎样才能从测验中得分?

时间:2013-10-20 10:37:04

标签: php mysql database

我对我的大学测验存在问题,我搜索了许多可能的解决方案,但没有一个能满足我的需求。我的测验是在数据库中运行的,我的问题在于结果页面。在测验结束时,最后一个问题在下一页上被捕获,但是没有显示总的正确答案,它只显示“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

0 个答案:

没有答案