title: Saving runtime created table rows using PHP and MySQL
body: <p>I am new to PHP. I am working on a form which has a table with thee columns that is two textboxes and and one combo box. I used JavaScript for generating the table rows and the same to delete it.
  I also referred one of your blogs for generating the rows. Now I am stuck on how to save the values from the the generated textbox to the MySQL database. Can anyone help me out? It would be a huge favor.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>&lt;?php
  session_start();
  if (!isset($_SESSION["username"])) {
      header("Location: unauthorize_access.php");
  }
  
  if(isset($_POST['submit']))
      {
  
          $length = count($_POST['testparameters']);
  
          for($i=0; $i&lt; $length; $i++){
              $TestParameters=$_POST['testparameters'][$i];
              if(!empty($TestParameters)){
              $InsertQuery = "INSERT INTO color(name) VALUES('$TestParameters')";
              $result=mysql_query($InsertQuery) or die(mysql_error());
          }
          }
          if(!$InsertQuery)
          {
              die('Could not enter data:' . mysql_error());
              }
      }
  ?&gt;
  
  
  
  
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
              &lt;title&gt;Makhtab&lt;/title&gt;
              &lt;link rel="stylesheet" type="text/css" href="form2/view.css" media="all"&gt;
                  &lt;script type="text/javascript" src="form2/view.js"&gt;&lt;/script&gt;
                  &lt;script type="text/javascript" src="form2/calendar.js"&gt;&lt;/script&gt;
                  &lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt; 
                      &lt;script type="text/javascript"&gt;
  
                              function deleteRow(row)
                              {
                              var i=row.parentNode.parentNode.rowIndex;
                              document.getElementById('POITable').deleteRow(i);
                              }
  
  
                              function insRow()
                              {
                              console.log( 'hi');
                              var x=document.getElementById('POITable');
                              var new_row = x.rows[1].cloneNode(true);
                              var len = x.rows.length;
                              new_row.cells[0].innerHTML = len;
  
                              var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
                              inp1.id += len;
                              inp1.value = '';
                              var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
                              inp2.id += len;
                              inp2.value = '';
                              x.appendChild( new_row );
                              }
                  &lt;/script&gt;
                                &lt;script type="text/javascript"&gt;
  
                              function deletezimRow(row)
                              {
                              var i=row.parentNode.parentNode.rowIndex;
                              document.getElementById('zimtable').deleteRow(i);
                              }
  
  
                              function inszimRow()
                              {
                              console.log( 'hi');
                              var x=document.getElementById('zimtable');
                              var new_row = x.rows[1].cloneNode(true);
                              var len = x.rows.length;
                              new_row.cells[0].innerHTML = len;
  
                              var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
                              inp1.id += len;
                              inp1.value = '';
                              var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
                              inp2.id += len;
                              inp2.value = '';
                              x.appendChild( new_row );
                              }
                  &lt;/script&gt;
                                      &lt;!--&lt;style type="text/css" title="currentStyle"&gt;
                                          @import "tran/css/layout-styles.css";
                                          @import "tran/css/themes/smoothness/jquery-ui-1.8.4.custom.css";
                                      &lt;/style&gt;--&gt;
                  &lt;script  type="text/javascript" src="tran/js/jquery-1.6.1.min.js"&gt;&lt;/script&gt;
                  &lt;script  type="text/javascript" src="tran/js/jquery-ui-1.8.14.custom.min.js"&gt;&lt;/script&gt;
  
                  &lt;script  type="text/javascript" src="tran/js/jq-ac-script.js"&gt;&lt;/script&gt;
                  &lt;/head&gt;
                  &lt;body id="main_body" &gt;
  
                      &lt;img id="top" src="form2/top.png" alt=""&gt;
                          &lt;div id="form_container"&gt;
  
                              &lt;h1&gt;&lt;a&gt;Untitled Form&lt;/a&gt;&lt;/h1&gt;
                              &lt;form id="makhtab" class="appnitro" enctype="multipart/form-data" method="post" action=""&gt;
                                  &lt;div class="form_description"&gt;
                                      &lt;h2&gt;Makhtab Details&lt;/h2&gt;
                                      &lt;!--&lt;p&gt;This is your form description. Click here to edit.&lt;/p&gt;--&gt;
                                  &lt;/div&gt;                      
                                  &lt;table border="0px" width="100%"&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;&lt;label class="description" for="element_1"&gt;Code&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="element_1" name="code" class="element text small" type="text" maxlength="6" value=""/&gt;&lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;&lt;label class="description" for="element_1"&gt;Name&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="element_1" name="name" class="element text large" type="text" maxlength="40" value=""/&gt; &lt;/td&gt;
                                      &lt;/tr&gt;
  
                                      &lt;tr&gt;
                                          &lt;td&gt;&lt;label class="description" for="element_1"&gt;Address&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="element_4_1" name="add1" class="element text large" value="" type="text"&gt;&lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="element_4_1" name="add2" class="element text large" value="" type="text"&gt;&lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="element_4_1" name="add3" class="element text large" value="" type="text"&gt;&lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;&lt;label class="description" for="element_1"&gt;City&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;select name="cmbcity" class="element text medium" style="font-size:18px;"/&gt;
                                              &lt;option value=""&gt;&lt;/option&gt;
                                              &lt;?php
                                              mysql_connect("localhost", "root", '') or die(mysql_error());
                                              mysql_select_db("webapp") or die(mysql_error());
                                              $result = mysql_query("SELECT `name` from city ") or die(mysql_error());
  
                                              while ($year = mysql_fetch_array($result)) {
  
                                                  echo "&lt;option value='$year[name]'&gt;$year[name]&lt;/option&gt;";
                                              }
                                              ?&gt;
                          &lt;/select&gt;
  
                                      &lt;/tr&gt;
                                                                          &lt;tr&gt;
                                          &lt;td&gt;&lt;label class="description" for="element_1"&gt;State&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;select name="cmbstate" class="element text medium" style="font-size:18px;"/&gt;
                                              &lt;option value=""&gt;&lt;/option&gt;
                                              &lt;?php
                                              mysql_connect("localhost", "root", '') or die(mysql_error());
                                              mysql_select_db("webapp") or die(mysql_error());
                                              $result = mysql_query("SELECT `name` from state ") or die(mysql_error());
  
                                              while ($year = mysql_fetch_array($result)) {
  
                                                  echo "&lt;option value='$year[name]'&gt;$year[name]&lt;/option&gt;";
                                              }
                                              ?&gt;
                          &lt;/select&gt;
                                                                          &lt;/tr&gt;
                                  &lt;/tr&gt;
                                  &lt;tr&gt;
                                      &lt;td&gt;&lt;label class="description" for="element_1"&gt;Country&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;select name="cmbcountry" class="element text medium" style="font-size:18px;"/&gt;
                                          &lt;option value=""&gt;&lt;/option&gt;
                                          &lt;?php
                                          mysql_connect("localhost", "root", '') or die(mysql_error());
                                          mysql_select_db("webapp") or die(mysql_error());
                                          $result = mysql_query("SELECT `name` from country ") or die(mysql_error());
  
                                          while ($year = mysql_fetch_array($result)) {
  
                                              echo "&lt;option value='$year[name]'&gt;$year[name]&lt;/option&gt;";
                                          }
                                          ?&gt;
                                          &lt;/select&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;&lt;label class="description" for="element_1"&gt;Telephone&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="element_1" name="country" class="element text medium" type="text" maxlength="255" value=""/&gt;&lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;&lt;label class="description" for="element_1"&gt;Mobile&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="element_1" name="country" class="element text medium" type="text" maxlength="10" value=""/&gt;&lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;br /&gt;
                                      &lt;/tr&gt;
  
                                      &lt;/table&gt;
  
                                           &lt;tr&gt;
                                      &lt;br /&gt;&lt;p style="border-bottom: 1px dotted #ccc;"&gt;&lt;/p&gt;&lt;br /&gt;
                                      &lt;div class="form_description"&gt;
                                          &lt;h2&gt;Zimmedar Details&lt;/h2&gt;
                                          &lt;!--&lt;p&gt;This is your form description. Click here to edit.&lt;/p&gt;--&gt;
                                      &lt;/div&gt;
                                  &lt;/tr&gt;
                                           &lt;input type="button" id="addmorezimbutton" value="Add zimmedar" onclick="insRow()"/&gt;&lt;br/&gt;&lt;br/&gt;
                                      &lt;table id="zimtable" border="0px" size="100px" cellspacing="0" cellpadding="2"&gt;
                                          &lt;tr&gt;
                                              &lt;td width="10" align="center"&gt;&lt;label class="description"&gt;Srno.&amp;nbsp;&lt;/label&gt;&lt;/td&gt;
                                              &lt;td width="118"&gt;&lt;label class="description"&gt;Zimmedar Name&lt;/label&gt;&lt;/td&gt;
                                              &lt;td width="118"&gt;&lt;label class="description"&gt;Mobile&lt;/label&gt;&lt;/td&gt;
  
                                              &lt;td width="10"&gt;&lt;/td&gt;
                                              &lt;td width="10" align="center"&gt;&lt;/td&gt;
                                          &lt;/tr&gt;
                                          &lt;tr&gt;
                                              &lt;td&gt;1&lt;/td&gt;
                                              &lt;td width="210"&gt;&lt;input  size="40" maxlenght="40" type="text" id="zimame[]"/&gt;&lt;/td&gt;
                                              &lt;td width="80"&gt;&lt;input  size="10" maxlenght="10" type="text" id="zimmob[]"/&gt;&lt;/td&gt;
  
                                              &lt;!--&lt;td&gt;&lt;input  size="45" maxlenght="10" type="text" id="momob"/&gt;&amp;nbsp;&lt;/td&gt;--&gt;
                                              &lt;td&gt;&lt;input type="button" id="addzimmore" value="+" onclick="inszimRow()"/&gt;&lt;/td&gt;
                                              &lt;td align="center"&gt;&lt;input type="button" id="delzimbutton" value="-" onclick="deletezimRow(this)"/&gt;&lt;/td&gt;
                                          &lt;/tr&gt;
  
                                      &lt;/table&gt;
  
                                                                           &lt;tr&gt;
                                      &lt;br /&gt;&lt;p style="border-bottom: 1px dotted #ccc;"&gt;&lt;/p&gt;&lt;br /&gt;
                                      &lt;div class="form_description"&gt;
                                          &lt;h2&gt;Muallim Details&lt;/h2&gt;
                                          &lt;!--&lt;p&gt;This is your form description. Click here to edit.&lt;/p&gt;--&gt;
                                      &lt;/div&gt;
                                  &lt;/tr&gt;
                                           &lt;input type="button" id="addmorePOIbutton" value="Add Muallims" onclick="insRow()"/&gt;&lt;br/&gt;&lt;br/&gt;
                                      &lt;table id="POITable" border="0px" size="100px" cellspacing="0" cellpadding="2"&gt;
                                          &lt;tr&gt;
                                              &lt;td width="10" align="center"&gt;&lt;label class="description"&gt;Srno.&amp;nbsp;&lt;/label&gt;&lt;/td&gt;
                                              &lt;td width="118"&gt;&lt;label class="description"&gt;Moallim Name&lt;/label&gt;&lt;/td&gt;
                                              &lt;td width="118"&gt;&lt;label class="description"&gt;Mobile&lt;/label&gt;&lt;/td&gt;
                                              &lt;td width="118"&gt;&lt;label class="description"&gt;Qabiliyat&lt;/label&gt;&lt;/td&gt;
                                              &lt;td width="10"&gt;&lt;/td&gt;
                                              &lt;td width="10" align="center"&gt;&lt;/td&gt;
                                          &lt;/tr&gt;
                                          &lt;tr&gt;
                                              &lt;td&gt;1&lt;/td&gt;
                                              &lt;td width="210"&gt;&lt;input  size="40" maxlenght="40" type="text" id="moname[]"/&gt;&lt;/td&gt;
                                              &lt;td width="80"&gt;&lt;input  size="10" maxlenght="10" type="text" id="momob[]"/&gt;&lt;/td&gt;
                                              &lt;td&gt;&lt;select name="cmbmuallim[]" class="element text large" style="font-size:18px;"/&gt;
                                                  &lt;option value=""&gt;&lt;/option&gt;
                                                  &lt;?php
                                                  mysql_connect("localhost", "root", '') or die(mysql_error());
                                                  mysql_select_db("webapp") or die(mysql_error());
                                                  $result = mysql_query("SELECT `name` from qabiliyat ") or die(mysql_error());
  
                                                  while ($year = mysql_fetch_array($result)) {
  
                                                      echo "&lt;option value='$year[name]'&gt;$year[name]&lt;/option&gt;";
                                                  }
                                                  ?&gt;
                                                  &lt;/select&gt;&lt;/td&gt;
                                              &lt;!--&lt;td&gt;&lt;input  size="45" maxlenght="10" type="text" id="momob"/&gt;&amp;nbsp;&lt;/td&gt;--&gt;
                                              &lt;td&gt;&lt;input type="button" id="addmore" value="+" onclick="insRow()"/&gt;&lt;/td&gt;
                                              &lt;td align="center"&gt;&lt;input type="button" id="delPOIbutton" value="-" onclick="deleteRow(this)"/&gt;&lt;/td&gt;
                                          &lt;/tr&gt;
                                      &lt;/table&gt;
  
                                               &lt;br /&gt;&lt;p style="border-bottom: 1px dotted #ccc;"&gt;&lt;/p&gt;&lt;br /&gt;
                                               &lt;table border="0px" width="85%"&gt;
                                                   &lt;tbody&gt;&lt;tr&gt;
                                                           &lt;td width="105"&gt;&lt;label class="description"&gt;No. of Students&lt;/label&gt;&lt;/td&gt;
                                                           &lt;td width="65"&gt;&lt;input type="text" name=stu" size="5" maxlength="5"&gt;&lt;/input&gt;&lt;/td&gt;
                                                           &lt;td width="105"&gt;&lt;label class="description"&gt;No. of Batches&lt;/label&gt;&lt;/td&gt;&lt;td width="14"&gt;&lt;input type="text" name="batch" size="5" maxlength="3"&gt;&lt;/input&gt;&lt;/td&gt;
                                                       &lt;/tr&gt;
                                                       &lt;tr&gt;
  
  
                                                       &lt;/tr&gt;
                                                   &lt;/tbody&gt;
  
  
  
                                               &lt;/table&gt;     
                                      &lt;/div&gt;
                                      &lt;/div&gt;
                              &lt;/form&gt;
  
                              &lt;/table&gt;    
                              &lt;/form&gt;
  
                              &lt;/body&gt;
                              &lt;/html&gt;
  </code></pre>