0

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:

enter image description here

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

    }
}
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
ana
  • 417
  • 2
  • 10
  • You are not defining it anywhere in the script. You're just calling it in the $query. – Chad K Aug 16 '18 at 14:37
  • Also, please use prepared statements – Chad K Aug 16 '18 at 14:37
  • @Chadk I'm not sure where to define it in the script. I keet getting the same error: `Undefined variable: solution_number` Can you help me? – ana Aug 16 '18 at 14:41
  • You could select @ solution_number = solution_number from solution where exercise_fk_id = $id. that should give you the solution number and store it in the sql variable @ solution_number (no space between @ and solution_number. Also, That's how it works on SQL Server, Not sure about Mysql) – Chad K Aug 16 '18 at 15:42

0 Answers0