Incrementing scores error

Im a student and new to php, and Im currently working on a Skill Assessment project. The problem with my codes is that the scores still increment even the answers are wrong.

I hope someone can correct my codes. Thank you!

The strand is the question type which are ABM, HUMSS, STEM, GAS and 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;
}

Display the value of $row[‘rightAnswer’] .
Is it what it should be?

Pardon me sir but what do you mean to say? The codes work, it displays the scores but the problem is, even if the answer is wrong, its still counted. I only have here 1 question on each category since im still working on it, and if i intentionally answer the questions wrong, it still shows 1 point on every category. I dont have an idea where the problem is…

I mean that say that to understand why the code executes in ways you don’t expect, a good method is to check the values of the variables in certain points of the script.

Is there a reason you are over-riding the $query2 variable?

$query2 = "SELECT strand FROM tquestions";
$row = mysqli_fetch_array($result, MYSQLI_ASSOC); 

$query2 = "SELECT strand FROM tquestions WHERE test_id = $qID";

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.