vendredi 20 août 2021

PHP/MYSQL: Multiple Choice Quiz - not updating responses

I have a form that outputs:

  1. Quiz_Assign_ID (Combines User_ID with Quiz ID)
  2. Question ID
  3. Checkboxes for inputting the response (Option A, Option B, Option C)
<form method="post" action="LP_Quiz_Student_Quiz_Responses.php">
      <table>
          <tr>
              <td>
              &nbsp;</td>
              <td>
              <input name="quiz_assign_id" type="hidden" value=" <?php echo $row["quiz_assign_id"]; ?>" />&nbsp;<input name="quiz_question_id" type="hidden" value=" <?php echo $row["quiz_question_id"]; ?>" /></td>
              <td>&nbsp;</td>
          </tr>
          <tr>
              <td>
              <h4>Question <?php echo $i++; ?>  </h4>
              </td>
              <td>
              <h4><?php echo $row["question"]; ?>&nbsp;</h4>
              </td>
              <td>&nbsp;</td>
          </tr>
          <tr>
              <td>
              <h4>A</h4>
              </td>
              <td><?php echo $row["option1"]; ?>&nbsp;</td>
              <td>
            
                  <input name="Response[]" type="checkbox" value="Option A" /></td>
          </tr>
          <tr>
              <td>
              <h4>B</h4>
              </td>
              <td><?php echo $row["option2"]; ?>&nbsp;</td>
              <td>
            
                  <input name="Response[]" type="checkbox" value="Option B" /></td>
          </tr>
          <tr>
              <td>
              <h4>C</h4>
              </td>
              <td><?php echo $row["option3"]; ?>&nbsp;</td>
              <td>
            
                  <input name="Response[]" type="checkbox" value="Option C" /></td>
          </tr>
     
     
      
       </table>
    
    
   <?php
      
      
      }
      
      ?>
     <input name="Submit1" type="submit" value="submit" />
      </form>

Upon submission I run the following script, which captures the response but does not get the question_id and Quiz_Assign_ID and does not update the values in the database:

<?php
    


    
    if(!empty($_POST['Response'])) {
    
    foreach ($_POST['Response'] as $value)  {



    $quiz_assign_id=trim($_POST['quiz_assign_id']); 
    $quiz_question_id=$_POST['quiz_question_id'];   
    
    
        echo $value;
         echo $quiz_question_id;
echo $quiz_question_id; echo 'successfully assigned! <br>';


    
    
    $stmt = $conn -> prepare('UPDATE Quiz_Assign_Student_Question SET response = ? WHERE quiz_assign_id = ? and quiz_question_id =? ');
    
    
    if (
        $stmt &&
        $stmt -> bind_param('sss', $value, $quiz_assign_id, $quiz_question_id) &&
        $stmt -> execute() &&
        $stmt -> affected_rows === 1
    ) {
        echo "<script>
         alert('Responses submitted!'); 
         window.history.go(-3);
         </script>";
        
        
    } else {
        echo"<script>
         window.history.go(-3);
         </script>";

    }
    }
    }
    
    
    
    ?>  

I have been playing with it for hours, but with no luck.




Aucun commentaire:

Enregistrer un commentaire