Dza Dza - 3 years ago 117
PHP Question

Incrementing scores error

QUESTION UPDATE

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;
}

Answer Source

You don't fecth the rows for 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); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download