title: Multiple forms and multiple submissions with one submit button
body: <p>At the moment, I'm using a loop to read the 'questions' and 'answers' fields from my postgresql "PDPC".considerations table and reflecting them on the form. I'd like to update them all with one submit button.  </p>
  
  <p>I refered to <a href="https://stackoverflow.com/questions/9596379/php-mysql-multiple-forms-and-multiple-submits-on-single-page">PHP MySQL Multiple Forms and Multiple Submits on single page</a> and tried using the <code>&lt;input type="hidden"&gt;</code>, arrays and <code>while</code> loop but the form either does not execute or it does not correctly update all the forms.</p>
  
  <p>I think the error is around the <code>$_POST</code> (at the top) and the HTML form (at the bottom). (Sorry if the codes are messy, it is my first time with PHP, HTML and Postgres). Thank you!</p>
  
  <pre><code>&lt;?php
  
  // Include config file
  require_once "config.php";
  
  // Define variables and initialize with empty values
  $question = $answer = "";
  $question_err = $answer_err = "";
  
      // Processing form data when form is submitted
   if(isset($_POST["consideration_no"]) &amp;&amp; !empty($_POST["consideration_no"])){
      $counter = 0;
      // Get hidden input value
      //$consideration_no = $_POST['consideration_no'];
      $dg_no = $_POST['dg_no'];
      $consideration_no = $_POST['consideration_no'];
      $answer = $_POST['answer'];
  
      // Check input errors before inserting in database
      if(empty($answer_err)){
  
          while ($counter&lt;5){ 
          // Validate address address
          $input_answer = trim($_POST["answer"]);
          if(empty($input_answer)){
              $answer_err = "Please enter an answer.";     
          } else{
              $answer1[$counter] = $input_answer;
          }   
  
          // Prepare an update statement
          $sql = 'UPDATE "PDPC".consideration SET answer=:answer WHERE consideration_no = :consideration_no';
          if($stmt = $pdo-&gt;prepare($sql)){
  
              $stmt-&gt;bindParam(":answer", $param_answer);
              $stmt-&gt;bindParam(":consideration_no", $param_consideration_no);
  
              //Set Parameter
              $param_answer = $answer1[$counter];
              $param_consideration_no = $consideration_no[$counter];
  
              // Attempt to execute the prepared statement
              $stmt-&gt;execute();
              $counter++;
              }
          }
                  if($stmt-&gt;execute()){
                  // Records updated successfully. Redirect to landing page
                  header("location: home1.php?dm_no=".$_GET["dm_no"]);
                  exit();
              } else{
                  echo "Something went wrong. Please try again later.";
              }
          // Close statement
          unset($stmt);
      }
  
      // Close connection
      unset($pdo);
  } else{
      // Check existence of dg_no parameter before processing further
      if(isset($_GET["dg_no"]) &amp;&amp; !empty(trim($_GET["dg_no"]))){
          // Get URL parameter
          $dg_no =  trim($_GET["dg_no"]);
  
          // Prepare a select statement
          $sql = 'SELECT * FROM "PDPC".consideration WHERE (dg_fkey = :dg_no AND code_no = 1)';
          if($stmt = $pdo-&gt;prepare($sql)){
              // Bind variables to the prepared statement as parameters
              $stmt-&gt;bindParam(":dg_no", $param_no);
  
              // Set parameters
             //$param_no = $dg_no;
              $param_no = trim($_GET["dg_no"]);
  
  
              // Attempt to execute the prepared statement
              if($stmt-&gt;execute()){
                  if($stmt-&gt;rowCount() &gt; 0){
  
                  SubSection($subsection1_1); //Collection Purpose Section
  
                      while($row = $stmt-&gt;fetch()){
                      // Retrieve individual field value
                      $consideration_no = $row["consideration_no"];
                      $question = $row["question"];
                      $answer = $row["answer"];
                      echo "&lt;a href='considerationupdate.php?consideration_no=". $row['consideration_no'] ."' title='Update Data Map' data-toggle='tooltip'&gt;&lt;span class='glyphicon glyphicon-pencil'&gt;&lt;/span&gt;&lt;/a&gt;";
  
                      //...time to show the questions and answers with the while loop...
                      ?&gt;
                            &lt;form action="&lt;?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?&gt;" method="post"&gt;
                              &lt;div class="form-group &lt;?php echo (!empty($answer_err)) ? 'has-error' : ''; ?&gt;"&gt;
                                  &lt;label&gt;&lt;?php echo $question; ?&gt;&lt;/label&gt;
                                  &lt;input type="text" name="answer" class="form-control" value="&lt;?php echo $answer; ?&gt;"&gt;
                                  &lt;span class="help-block"&gt;&lt;?php echo $answer_err;?&gt;&lt;/span&gt;
                                  &lt;input type="hidden" name="answer1[]" id = "$answer1" value="&lt;?php echo $answer; ?&gt;"/&gt;
                                  &lt;input type="hidden" name="consideration_no[]" id = "consideration_no" value="&lt;?php echo $consideration_no; ?&gt;"/&gt;
                                  &lt;input type="hidden" name="dg_no" value="&lt;?php echo $dg_no; ?&gt;"/&gt;
                              &lt;/div&gt;
                              &lt;input type="submit" class="btn btn-primary" value="Submit"&gt;
                      &lt;?php
  
                      }
                      ?&gt;
                              &lt;input type="submit" class="btn btn-primary" value="Submit"&gt;
                              &lt;a href="javascript:history.go(-1)" class="btn btn-default"&gt;Cancel&lt;/a&gt;
                             &lt;/form&gt;
                          &lt;/div&gt;
                      &lt;?php
  
                  } 
              } 
              else{
                  echo "Oops! Something went wrong. Please try again later.";
              }
          }
          // Close statement
          unset($stmt);
  
          // Close connection
          unset($pdo);
      }  
      else{
          // URL doesn't contain dg_no parameter. Redirect to error page
          header("location: error.php");
          exit();
      }
  }
  ?&gt;
  </code></pre>
  
  <p>It should read the questions and answers from the DB table, show it on the forms as the label and text fields (working), and the user should be able to update the form after editing the text fields and clicking submit (Not working properly).</p>
  
  <p><a href="https://i.stack.imgur.com/q5EbT.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q5EbT.jpg" alt="Wanted results after submit"></a></p>
  
  <p><a href="https://i.stack.imgur.com/V3xAp.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/V3xAp.jpg" alt="Actual result (it takes the bottom field&#39;s answer for all)"></a></p>