I am have been attempting to compare values selected from a MYSQL to a PHP array. My problem is that I can print out the array to see the values but i am not sure how to compare the MYSQL values to what is in the array. As in the code i have provided i have attempted to use an if else statement but does not work how i expected to as it does not compare the answer but still echos out the use response is incorrect . I would really appreciate it someone could look over my code and give me some guidance as to where i am going wrong.
I have included the two php pages i am currently working with
Answer Question Page
<?php
require_once ('mysqli_connect.php');
$q = "SELECT * FROM multiplechoice_db ORDER BY RAND() LIMIT 2";
$result = @mysqli_query ($dbcon, $q); // Run the query
if ($result) { // If it ran OK, display the records
?>
<form action="answerquiz.php" method="GET">
<table>
<tr>
<td><b> For Each Question There will Only Be One Possible Answer</b> </td>
<br>
</tr>
<?php
// Fetch and print all the records
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$question = $row['question'];
$question_id = $row['question_id'];
$option_1 = $row['option_1'];
$option_2 = $row['option_2'];
$option_3 = $row['option_3'];
$option_4 = $row['option_4'];
?>
<tr>
<tr>
<td>
<br>
<br>
<ol><li><?php echo $question;?></li></ol>
1.<input type="radio" name = "question_id[<?php echo $question_id;?>]" value= "1" /><?php echo $option_1;?>
<br>
2.<input type="radio" name = "question_id[<?php echo $question_id;?>]" value= "2" /><?php echo $option_2;?>
<br>
3.<input type="radio" name = "question_id[<?php echo $question_id;?>]" value= "3" /><?php echo $option_3;?>
<br>
4.<input type="radio" name = "question_id[<?php echo $question_id;?>]" value= "4" /><?php echo $option_4;?>
<br>
<td>
<td>
<td>
<td>
</td>
</tr>
<?php
}
?>
</table>
<br>
<input type ="submit" value="Submit Quiz">
</form>
<?php
}
?>
answer_quiz page
require ('mysqli_connect.php');
if(isset($_REQUEST['question_id']) ){
$arr_question_id = $_REQUEST['question_id'];
//$question = '3';
// print_r($arr_question_id);
$learner_response= "SELECT question_id, answer FROM multiplechoice_db WHERE question_id = 'question_id' AND answer = 'answer'";
$result=@mysqli_query($dbcon, $learner_response);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$question_id = $row['question_id'];
$answer = $row['answer'];
}
$correct_answer=0;
$incorrect_answer=0;
//if ($result == FALSE)
//{
//die(mysqli_error($dbcon));
//}
foreach($arr_question_id as $question_id=>$option_value){
print " Question id: " .$question_id.
"Learner Response: ". $option_value. "\
";
}
//while ($row = mysqli_fetch_array($result)) {
foreach($arr_question_id as $question_id=>$option_value){
if($row['answer'] == $option_value){
echo ' You have answered the Question Correct';
$correct_answer++;
}else{
echo ' You have answered the Question Incorrect';
$incorrect_answer++;
}
"Numbers of correct answer : ". $correct_answer."<br>";
"Numbers of correct answer : ". $incorrect_answer."<br>";
}
}