title: Saving PHP form with foreach loop to MYSQL
body: <p>I'm trying to save question options input into a table in database. However, I can only save the last question inputs into db but not the rest before that. </p>
  
  <p>I'm currently using foreach loop to come out with textfield inputs based on the question structure table. </p>
  
  <p>I don't really know how to explain in words, let me show you my codes.
  I'm new in this field, hope you guys understand and help me out :)</p>
  
  <ul>
  <li><p>foreachtest.php </p>
  
  
  
  <pre><code>&lt;?php
  $user_name = "root";
  $password = "";
  $database = "leadership_program";
  $server = "localhost";
  
  $db_handle = mysql_connect($server, $user_name, $password);
  $db_found = mysql_select_db($database, $db_handle);
  
  $result = mysql_query("SELECT * FROM question_structure_tb", $db_handle);
  
  $row = mysql_fetch_assoc($result);
  
  
  mysql_close($db_handle);
  $survey_name = $row['survey_name'];
  $row2 = array_slice($row, 2);
  
  
  
  echo 'Survey Name: '. $survey_name ;
  
  foreach ($row2 as $v) {
      //echo "$v\n";
  
  
      if ($v != NULL) {
  
          echo '&lt;p&gt;Question:&lt;/p&gt;';
  
          echo '&lt;p&gt;Enter Question: &lt;input type=\"text\" name=\"question\" size=\"200\" maxLength=\"300\" value&gt;&lt;/p&gt;';
  
  
          if ($v === 'radio') {
  
              echo"&lt;p&gt;Option 1:&lt;input type=\"text\" name=\"optionone\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;Option 2:&lt;input type=\"text\" name=\"optiontwo\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;Option 3:&lt;input type=\"text\" name=\"optionthree\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;Option 4:&lt;input type=\"text\" name=\"optionfour\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;Option 5:&lt;input type=\"text\" name=\"optionfive\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;Option 6:&lt;input type=\"text\" name=\"optionsix\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;Option 7:&lt;input type=\"text\" name=\"optionseven\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;&lt;input type=\"hidden\" name=\"optioneight\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;&lt;input type=\"hidden\" name=\"optionnine\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;&lt;input type=\"hidden\" name=\"optionten\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;&lt;input type=\"hidden\" name=\"optioneleven\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;&lt;input type=\"hidden\" name=\"optiontwelve\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;&lt;input type=\"hidden\" name=\"optionthirteen\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;&lt;input type=\"hidden\" name=\"optionfourteen\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
  
          } else if ($v === 'checkbox') {
  
              echo"&lt;p&gt;Option 1:&lt;input type=\"text\" name=\"optionone\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;Option 2:&lt;input type=\"text\" name=\"optiontwo\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;Option 3:&lt;input type=\"text\" name=\"optionthree\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;Option 4:&lt;input type=\"text\" name=\"optionfour\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;Option 5:&lt;input type=\"text\" name=\"optionfive\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;Option 6:&lt;input type=\"text\" name=\"optionsix\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;Option 7:&lt;input type=\"text\" name=\"optionseven\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;Option 8:&lt;input type=\"text\" name=\"optioneight\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;Option 9:&lt;input type=\"text\" name=\"optionnine\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;Option 10:&lt;input type=\"text\" name=\"optionten\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;Option 11:&lt;input type=\"text\" name=\"optioneleven\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;Option 12:&lt;input type=\"text\" name=\"optiontwelve\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;Option 13:&lt;input type=\"text\" name=\"optionthirteen\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
              echo"&lt;p&gt;Option 14:&lt;input type=\"text\" name=\"optionfourteen\" size=\"40\" maxLength=\"30\"&gt;&lt;/p&gt;";
          } else {
              echo "&lt;p&gt;this is comment or with comments.therefore no input field&lt;/p&gt;"; 
  
          }
      } else {
  
      }
      unset($v);
  }
  ?&gt;
  &lt;p&gt;&lt;input type="submit" value="Save" name="B1"&gt;&lt;/p&gt;
  </code></pre>
  
  </li>
  <li><p>foreachtest_save.php</p>
  
  <p>
  
  <pre><code>$user_name = "root";
  $password = "";
  $database = "leadership_program";
  $server = "localhost";
  
  $db_handle = mysql_connect($server, $user_name, $password);
  $db_found = mysql_select_db($database, $db_handle);
  
  
  if ($db_found) {
  
  
      $Questions = $_POST['question'];
  
  
      $SQL = "INSERT INTO question_table ('question_detail') VALUES ('$Questions')";
  
      $result = mysql_query($SQL); //execute the above 
  
      $Option1 = $_POST['optionone'];
      $Option2 = $_POST['optiontwo'];
      $Option3 = $_POST['optionthree'];
      $Option4 = $_POST['optionfour'];
      $Option5 = $_POST['optionfive'];
      $Option6 = $_POST['optionsix'];
      $Option7 = $_POST['optionseven'];
      $Option8 = $_POST['optioneight'];
      $Option9 = $_POST['optionnine'];
      $Option10 = $_POST['optionten'];
      $Option11 = $_POST['optioneleven'];
      $Option12 = $_POST['optiontwelve'];
      $Option13 = $_POST['optionthirteen'];
      $Option14 = $_POST['optionfourteen'];
  
  
  
      $SQL2 = "INSERT INTO option_table " .
        "(option_one,option_two, option_three, option_four,option_five, option_six, option_seven, option_eight, option_nine, option_ten, option_eleven, option_twelve, option_thirteen, option_fourteen)" .
          "VALUES" .
          "('$Option1','$Option2','$Option3','$Option4', '$Option5','$Option6','$Option7','$Option8','$Option9','$Option10','$Option11','$Option12','$Option13','$Option14' )";
  
      $result2 = mysql_query($SQL2); //execute the above 
  
      if ($result2) {
          print "Options added to the database";
      } else {
          print "Error: Options not added";
      }
      mysql_close($db_handle);
  } else {
  
      print "Database NOT Found ";
      mysql_close($db_handle);
  }
  ?&gt;
  </code></pre></li>
  </ul>