I'm working on a simple quiz. One question is displayed at a time with 3 possible answers on radio buttons. The answer for each question is stored in a table from a database called SOLUTION:
The problem is that when I run the program I get the following error: Undefined variable: solution_number.
Can you help me define the variable solution_number
in my radio buttons? I'm not sure how to do it.
Here is my code for the radio buttons:
<form action='' method='post'>
<input type="radio" name="choice" value= "1" /><img src="<?php echo $row["image_path_A"] ?>"/><br>
<input type="radio" name="choice" value= "2" /><img src="<?php echo $row["image_path_B"] ?>"><br>
<input type="radio" name="choice" value= "3" /><img src="<?php echo $row["image_path_C"] ?>"><br>
<p2>Select difficulty level:</p2>
<form action='' method='post'>
<select name="choose" id="choose">>
<option value="1" <?php if($row["difficulty"]=="1") { echo "selected"; } ?> >1</option>
<option value="2" <?php if($row["difficulty"]=="2") { echo "selected"; } ?> >2</option>
<option value="3" <?php if($row["difficulty"]=="3") { echo "selected"; } ?> >3</option>
<option value="4" <?php if($row["difficulty"]=="4") { echo "selected"; } ?> >4</option>
<option value="5" <?php if($row["difficulty"]=="5") { echo "selected"; } ?> >5</option>
</select>
<input class="buttonSubmit" type="submit" name="submit" value="Submit">
<?php
if ($next_question_id >= 0) {
?>
<a href="?id=<?php echo $next_question_id; ?>&order=<?php echo $next_question_order; ?>" class="buttonNext" >Next Question</a>
<?php
}
?>
</form>
This is the PHP: The error is here in ‘solution_number
’. As it is not defined.
<?php
if (isset($_POST['submit'])) {
$user_id = $_SESSION['user_id'];
$user_check_query = "SELECT * FROM users WHERE id='$user_id'";
if(isset($_POST['choice'], $_POST['choose'])){
$choice_answer=$_POST['choice'];
$difficulty=$_POST['choose'];
$query = "INSERT INTO answers
(exercise_id_fk, student_id, difficulty_change,
difficulty_student, choice_answer, correct_answer)
VALUES ('$id','$user_id',
(SELECT IF(difficulty='$difficulty','NO','YES')
FROM exercises
WHERE exercise_id=$id),
'$difficulty', '$choice_answer',
(SELECT IF(solution_number='$solution_number','1','0')
FROM solution WHERE exercise_id_fk=$id))";
$sql=mysqli_query($conn,$query);
}
}
?>