title: Generating HTML Form from database using PHP
body: <p>I'm building a basic website that will offer a quiz dynamically generated from a MySQL database.  Based on my current database schema, I'm having trouble understanding how I will generate the 'choices' to different questions in a Quiz Web App.  </p>
  
  <p>Here is the database schema:</p>
  
  <pre><code>CREATE TABLE user (
      user_id INT UNSIGNED PRIMARY KEY,
      username VARCHAR(32) NOT NULL UNIQUE,
      password VARCHAR(128) NOT NULL,
      ...
  ) Engine=InnoDB;
  
  CREATE TABLE quiz (
      quiz_id INT UNSIGNED PRIMARY KEY,
      title VARCHAR(64)
  ) Engine=InnoDB;
  
  CREATE TABLE question (
      question_id INT UNSIGNED PRIMARY KEY,
      quiz_id INT UNSIGNED NOT NULL,
      question VARCHAR(1024),
      FOREIGN KEY (quiz_id) REFERENCES quiz (quiz_id)
  ) Engine=InnoDB;
  
  CREATE TABLE question_choices (
      choice_id INT UNSIGNED PRIMARY KEY,
      question_id INT UNSIGNED NOT NULL,
      is_correct_choice TINYINT(1),
      choice VARCHAR(512),
      FOREIGN KEY (question_id) REFERENCES question (question_id)
  ) Engine=InnoDB;
  
  CREATE TABLE quiz_response (
      response_id INT UNSIGNED PRIMARY KEY,
      user_id INT UNSIGNED NOT NULL,
      question_id INT UNSIGNED NOT NULL,
      response INT UNSIGNED NOT NULL,
      is_correct TINYINT(1),
      answer_time FLOAT,
      UNIQUE KEY (user_id, question_id)
      FOREIGN KEY (user_id) REFERENCES user (user_id),
      FOREIGN KEY (question_id) REFERENCES question (question_id),
      FOREIGN KEY (response) REFERENCES question_choices (choice_id),
  ) Engine=InnoDB;
  </code></pre>
  
  <p>Here is the code I have produced so far in my quiz.php script:</p>
  
  <pre><code>// If this user has never taken this quiz, insert empty responses into the quiz_response table
      $query = "SELECT * FROM quiz_response WHERE user_id = '" . $_SESSION['user_id'] . "'";
      $data = mysqli_query($dbc, $query);
      if (mysqli_num_rows($data) == 0) {
          //First grab the list of questions to create empty responses
          //Grab all questions from question table
          //Rework code in the future to accommodate multiple quizes
          $query = "SELECT question_id from question";
          $data = mysqli_query($data, $query);
          $questionIDs = array();
          while ($row = mysqli_fetch_array($data)) {
              array_push($questionIDs, $row['question_id']);
          }
  
          // Insert empty response rows into the response table, one row per question
          foreach ($questionIDs as $question_id) {
              $query = "INSERT INTO quiz_response (user_id, question_id) VALUES ('" . $_SESSION['user_id']. "', '$question_id')";
              mysqli_query($dbc, $query);
          }   
      }
  
      // If the quiz form has been submitted, write the form responses to the database
      if (isset($_POST['submit'])) {
          // Write the quiz response rows to the response table
          foreach ($_POST as $response_id =&gt; $response) {
              $query = "UPDATE quiz_response SET response = '$response' WHERE response_id = '$response_id'";
              mysqli_query($dbc, $query);
          }
          echo '&lt;p&gt;Your responses have been saved.&lt;/p&gt;
      }
  
      // Grab the response data from the database to generate the form
      $query = "SELECT qr.response_id, qr.question_id, qr.response, q.question, quiz.quiz " . 
          "FROM quiz_response AS qr " . 
          "INNER JOIN question AS q USING (question_id) " . 
          "INNER JOIN quiz USING (quiz_id) " . 
          "WHERE qr.user_id = '" . $_SESSION['user_id'] . "'";
      $data = mysqli_query($dbc, $query);
      $responses = array();
      while ($row = mysqli_fetch_array($data)) {
          // Pull up the choices for each question
          $query2 = "SELECT choice_id, choice FROM question_choice " . 
              "WHERE question_id = '" . $row['question_id'] . "'";
          $data2 = mysqli_query($dbc, $query2);
          $choices = array();
          while ($row2 = mysqli_fetch_array($data2)) {
              array_push($choices, $row2);
          }
          // Rename choices 
  
  
  
  
          // Eventually push choices into $responses array
          // array_push($responses, $row);
      }
  
      mysqli_close($dbc);
  
      // Generate the quiz form by looping through the response array
      echo '&lt;form method="post" action="' . $_SERVER['PHP_SELF'] . '"&gt;';
      echo '&lt;h2&gt;' . $page_title . '&lt;/h2&gt;';
      $question_title = $responses[0]['question'];
      echo '&lt;label for="' . $responses[0][response_id'] . '"&gt;' . $responses[0]['question'] . '&lt;/label&gt;&lt;br /&gt;';
      foreach ($responses as $response) {
          // Only start a new question if the question changes
          if ($question_title != $response['question']) {
              $question_title = $response['question'];
              echo '&lt;br /&gt;&lt;label for="' . $response['response_id'] . '"&gt;' . $response['question'] . '&lt;/label&gt;&lt;br /&gt;';
          }
          // Display the choices
          // Choice 1
          // Choice 2
          // Choice 3
          // Choice 4
  
  
      }
      echo '&lt;br /&gt;&lt;br /&gt;';
      echo '&lt;input type="submit" value="Grade Me!" name="submit" /&gt;';
      echo '&lt;/form&gt;';
  </code></pre>
  
  <p>I'm having trouble pulling the choice options out of the question_choice table and using them to populate the form. Can I put the choice_id and choice columns into the <code>$responses</code> array and access them in the generating form section without renaming them? At this point I feel I need to rename. Any help would be greatly appreciated!</p>