没有获得测验脚本的无线电值和分数

时间:2015-03-22 10:27:23

标签: php html sql

我正在使用php和sql进行多项选择测验。当我参加测验并提交答案时,它不会显示分数。

这是脚本:

<?php
$con = mysql_connect("localhost", "ashu","ashua");
$db=mysql_select_db("quiz",$con) or die(mysql_error());

$display = mysql_query("SELECT * FROM quiz ORDER BY id");

if (empty($_POST['submit'])) {

    echo "<form method=post action=".$_SERVER['PHP_SELF'].">";
    echo "<table border=0>";

    while ($row = mysql_fetch_array($display)) {
        $id = $row["id"];
        $question = $row["question"];
        $opt1 = $row["opt1"];
        $opt2 = $row["opt2"];
        $opt3 = $row["opt3"];
        $answer = $row["answer"];

        echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
        echo "<tr><td>$opt1 <input type=radio name=q$id value=\"$opt1\"></td><td>$opt2 <input type=radio name=q$id value=\"$opt2\"></td><td>$opt3 <input type=radio name=q$id value=\"$opt3\"></td></tr>";
    }

    echo "</table>";
    echo "<input type='submit' value='See how you did' name='submit'>";
    echo "</form>";
}
elseif (!empty($_POST['submit'])) {
    $score = 0;
    $total = mysql_num_rows($display);

    while ($result = mysql_fetch_array($display)) {
        $answer = $result["answer"];
        $q = $result["q"];

        if ($q == $answer)  {
            $score++; 
        }

    }

    echo "<p align=center><b>You scored $score out of $total</b></p>";
    echo "<p>";

    if ($score == $total) {
        echo "Congratulations! You got every question right!";
    } elseif ($score/$total < 0.34) {
        echo "Oh dear. Not the best score, but don't worry, it's only a quiz.";
    } elseif ($score/$total > 0.67) {
        echo "Well done! You certainly know your stuff.";
    } else {
        echo "Not bad - but there were a few that caught you out!";
    }

    echo "</p>";

    echo "<p>Here are the answers:";

    echo "<table border=0>";
    $display = mysql_query("SELECT * FROM quiz ORDER BY id");
    while ($row = mysql_fetch_array($display)) {

        $question = $row["question"];
        $answer = $row["answer"];
        $q = $row["q"];

        echo "<tr><td><br>$question</td></tr>";

        if ($q == $answer)  {
            echo "<tr><td>&raquo;you answered ${$q}, which is correct</td></tr>";
        } elseif ($q == "") {
            echo "<tr><td>&raquo;you didn't select an answer. The answer is $answer</td></tr>";
        } else {
            echo "<tr><td>&raquo;you answered ${$q}. The answer is $answer</td></tr>";
        }
    }
    echo "</table></p>";
}

?>

当我提交或完成测验时,我得到结果

  

你得分为0

     亲爱的,亲爱的。不是最好的分数,但不要担心,这只是一个小测验。

     

以下是答案:   Q1:拉夫   »你没有选择答案。答案是1

     

Q2:ashuas   »你没有选择答案。答案是12

它只显示没有显示正确分数的正确答案,而且它显示你没有选择和回答。

2 个答案:

答案 0 :(得分:0)

您编写elseif (!empty($_POST['submit'])),但从不访问后续块中的$_POST。您是否至少不能使用$POST来检查发布的答案是否正确?

相反,您正在使用

检查发布的答案是否正确
while ($result = mysql_fetch_array($display)) {
    $answer = $result["answer"];
    $q = $result["q"];

    if ($q == $answer) {
        $score++; 
    }
}

即。仅通过查看数据库。

答案 1 :(得分:0)

以下是您的答案只需替换为您的代码

<?php
$con = mysql_connect("localhost", "ashu","ashua");
$db=mysql_select_db("quiz",$con) or die(mysql_error());

$display = mysql_query("SELECT * FROM quiz ORDER BY id");

if (empty($_POST['submit'])) {


echo "<form method=post action=".$_SERVER['PHP_SELF'].">";
echo "<table border=0>";

while ($row = mysql_fetch_array($display)) {

$id = $row["id"];
$question = $row["question"];
$opt1 = $row["opt1"];
$opt2 = $row["opt2"];
$opt3 = $row["opt3"];
$answer = $row["answer"];

echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
echo "<tr><td>$opt1 <input type=radio name=q$id value=\"$opt1\"></td><td>$opt2      <input type=radio name=q$id value=\"$opt2\"></td><td>$opt3 <input type=radio name=q$id value=\"$opt3\"></td></tr>";

}

echo "</table>";
echo "<input type='submit' value='See how you did' name='submit'>";
echo "</form>";

}

elseif  (!empty($_POST['submit'])) 

{



$display = mysql_query("SELECT * FROM quiz ORDER BY id");

$score = 0;
$total = mysql_num_rows($display);
    while ($result = mysql_fetch_array($display)) 


    {
            $id = $result["id"];
            $q = $_POST["q$id"];


        $answer = $result["answer"];



    if ($q == $answer) 
    {
    $score++; 
    }

}

echo "<p align=center><b>You scored $score out of $total</b></p>";
echo "<p>";

if   ($score == $total) {
echo "Congratulations! You got every question right!";
}
elseif ($score/$total < 0.34) {
echo "Oh dear. Not the best score, but don't worry, it's only a quiz.";
}
elseif ($score/$total > 0.67) {
echo "Well done! You certainly know your stuff.";
}
else {
echo "Not bad - but there were a few that caught you out!";
}

echo "</p>";

echo "<p>Here are the answers:";

echo "<table border=0>";
$display = mysql_query("SELECT * FROM quiz ORDER BY id");
while ($row = mysql_fetch_array($display)) {

$question = $row["question"];
$answer = $row["answer"];
$q = $row["q"];

echo "<tr><td><br>$question</td></tr>";

if ($q == $answer) 
    {
    echo "<tr><td>&raquo;you answered ${$q}, which is correct</td></tr>";
    }
elseif ($q == "") {
echo "<tr><td>&raquo;you didn't select an answer. The answer is $answer</td>     </tr>";
}
else {
echo "<tr><td>&raquo;you answered ${$q}. The answer is $answer</td></tr>";
}

}
echo "</table></p>";



}

?>