1

我是 PHP 新手,我需要一些帮助。我似乎无法为学生获得正确的分数。

场景:

  • 输入对应分数的考试答案(答案键)
  • 输入该考试的学生答案
  • 获取学生的总分。

我的PHP代码:

for ($count = 1; $count <= $num_ans; $count++)
{
    $answer = $_POST['answer'][($count + 1) - 1];
    $sqlB = "SELECT * FROM paper WHERE id=$count and test_name = '$test_name' and subject='$subject'";
    $qryB = mysql_query($sqlB);
    $rowB = mysql_fetch_array($qryB);
    $anskey = $rowB['answer'];
}
if ($answer = $anskey)
{
    $sqlA = "SELECT points FROM paper WHERE test_name = '$test_name' and subject='$subject' and answer='$answer'";
    $qryA = mysql_query($sqlA);
    while ($rowA = mysql_fetch_array($qryA))
    {
        $correctAns += $rowA['points'];
    }
}
4

2 回答 2

0

尝试

 for ($count=1; $count<=$num_ans; $count++) {            
        $answer = $_POST['answer'][$count]; 
        $sqlB = "SELECT answer FROM paper WHERE id=$count and test_name = '$test_name' and    subject='$subject'";
        $qryB = mysql_query($sqlB);
        $rowB = mysql_fetch_array($qryB);
        $anskey = $rowB['answer'];

        if ($answer == $anskey) { 
        $sqlA = "SELECT points FROM paper WHERE test_name = '$test_name' and subject='$subject' and answer='$answer'";
        $qryA = mysql_query($sqlA);
         //considering each unique answer would have specific points, while-loop is not required
        //while($rowA = mysql_fetch_array($qryA)) {
        $correctAns += $rowA['points'];
       // }
        }  
} //end of for-loop
于 2012-09-18T19:33:06.467 回答
0

我认为您的数据库查询中缺少某些内容。

$query = "SELECT points FROM paper WHERE test_name = '$test_name' and subject='$subject' and answer='$answer'";
$qryA = mysql_query($query);
while($rowA = mysql_fetch_array($qryA)) {
   $correctAns += $rowA['points'];
}

请检查 :

  $rowA['points'] = ??// if any value return from db or not.

因为 SQL 查询,请传递问题 id/(行中唯一的东西) answer = '$answer' 可能是重复的,所以它不能返回实际值。

希望你能理解。

于 2012-09-18T19:45:59.667 回答