增加分数错误

时间:2017-10-01 05:49:39

标签: php mysqli

问题更新

我是一名学生,也是php的新手,我目前正致力于技能评估项目。如果我有意回答所有错误的问题,将显示的分数必须为0,但问题是分数仍然增加。我该怎么办?我希望有人可以帮我纠正我的代码。谢谢!

该链是ABM,HUMSS,STEM,GAS和TVL的问题类型

if(isset($_POST['submit'])) {
 $ans = $_POST['ans'];
 $abmscore = 0;
 $humssscore = 0;
 $stemscore = 0;
 $gasscore = 0;
 $tvlscore = 0;

if( !empty($ans)):
  foreach($ans as $qID => $qVal) {
    $qID =  (int) $qID;
    $qVal = (int) $qVal;

    $query1= "SELECT COUNT(*) AS rightAnswer FROM tquestions WHERE test_id = $qID AND correctanswer = $qVal";
    $result= mysqli_query($conn, $query1);
    $query2 = "SELECT strand FROM tquestions";
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC); 

    $query2 = "SELECT strand FROM tquestions WHERE test_id = $qID";
    $result2 = mysqli_query($conn, $query2);
    $row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC); 
    $strand =$row2['strand'];

    if($row['rightAnswer']) {
       if($strand == 'ABM' )  {
          $abmscore++;
       }

       elseif ($strand == 'HUMSS' ) {
          $humssscore++;
       }

       elseif ($strand == 'STEM' ) {
          $stemscore++;
       }

       elseif ($strand == 'GAS' ) {
          $gasscore++;
       }

       elseif ($strand == 'TVL' ) {
          $tvlscore++;
       }        
    }
  }
  endif;
}

1 个答案:

答案 0 :(得分:0)

你没有为query2

创建行
  $query1= "SELECT COUNT(*) AS rightAnswer 
            FROM tquestions WHERE test_id = $qID AND correctanswer = $qVal";
  $result= mysqli_query($conn, $query1);
  // 
  $query2 = "SELECT strand FROM tquestions";
  $result2 = mysqli_query($conn, $query2);
  $row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC); 
  $strand =$row2['strand'];
  //
  $row = mysqli_fetch_array($result, MYSQLI_ASSOC);