title: How can i insert dynamic textbox values into database table on same ID using php?
body: <p>I need to insert dynamic text box values into database table on same id,now I can possible for inserting values on different user id only.</p>
  
  <p>when i am trying to insert dynamic text boxes values into database table on id 1,the dynamic text boxes values going to different id like 2,3,4 and so on.And also becoming zero field values of id 1.<a href="http://i.stack.imgur.com/MPWTt.png" rel="nofollow">http://i.stack.imgur.com/MPWTt.png</a> </p>
  
  <p>here's my html and javascript code:</p>
  
  <p>file name:index.php</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  
      &lt;head&gt;
      &lt;SCRIPT src="http://code.jquery.com/jquery-2.1.1.js"&gt;&lt;/SCRIPT&gt;
          &lt;style&gt;
          table, th, td {
          border: 1px solid black;
          border-collapse: collapse;
          }
          th, td {
          padding: 5px;
          }
          table{
          background-image:  #eee;
          }
  
          &lt;/style&gt;
  
      &lt;/head&gt;
  
  &lt;body&gt;
  
      &lt;form name="registration" id="regis" method="post" enctype="multipart/formdata"&gt;
          &lt;table&gt;
  
                  &lt;th colspan="1"&gt;
                  &lt;th&gt;Details Mahall members&lt;/th&gt;
                  &lt;/th&gt;
  
              &lt;tr&gt;
                  &lt;td&gt;Name of the person:&lt;/td&gt;
                  &lt;td&gt;
                      &lt;input type="text" name="first_name1" id="fname1" placeholder="Enter name" value=""/&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
  
  
              &lt;tr&gt;
                  &lt;td&gt;Name of the father:&lt;/td&gt;
                  &lt;td&gt;
                      &lt;input type="text" name="first_name2" id="fname2" placeholder="Enter name" value=""/&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;tr&gt;
                  &lt;td&gt;Name of the mother:&lt;/td&gt;
                  &lt;td&gt;
                      &lt;input type="text" name="first_name3" id="fname3" placeholder="Enter name" value=""/&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
  
  
  
              &lt;tr&gt;
                  &lt;td&gt;Details of family members:&lt;/td&gt;&lt;td&gt;&lt;/td&gt;
  
              &lt;/tr&gt;
  
              &lt;tr&gt;
                  &lt;td&gt;&lt;/td&gt;
                  &lt;td&gt;
                      &lt;table width="100%"&gt;
                          &lt;tr&gt;
                              &lt;th&gt;&lt;center&gt;No:&lt;/center&gt;&lt;/th&gt;&lt;th&gt;&lt;center&gt;Name&lt;/center&gt;&lt;/th&gt;&lt;th&gt;&lt;center&gt;Age&lt;/center&gt;&lt;/th&gt;&lt;th&gt;&lt;center&gt;Relation&lt;strong&gt;&lt;/th&gt;&lt;th&gt;&lt;center&gt;Occupation&lt;strong&gt;&lt;/th&gt;
                          &lt;/tr&gt;
                      &lt;/table&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;tr&gt;
                  &lt;td&gt;&lt;/td&gt;
  
                  &lt;td&gt;
                  &lt;DIV id="product"&gt;
                          &lt;DIV class="product-item float-clear" style="clear:both;"&gt;
  
                              &lt;?php require_once("sub.php") ?&gt;
  
                          &lt;/DIV&gt;
                      &lt;/DIV&gt;&lt;/td&gt;&lt;/tr&gt;
                  &lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt; &lt;DIV class="btn-action float-clear"&gt;
                      &lt;input type="button" name="add_item" value="Add More" onClick="addMore();" /&gt;
                      &lt;input type="button" name="del_item" value="Delete" onClick="deleteRow();" /&gt;
                      &lt;span class="success"&gt;&lt;?php if(isset($message)) { echo $message; }?&gt;&lt;/span&gt;
                  &lt;/DIV&gt;
                  &lt;/td&gt;
  
  
              &lt;/tr&gt;
  
              &lt;tr&gt;
              &lt;td&gt;
  
                  &lt;input type="submit" name="save" value="Submit"/&gt;
                  &lt;input type="reset" value="cancel"/&gt;
  
              &lt;/td&gt;&lt;td&gt;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
  
  
      &lt;/form&gt;
      &lt;SCRIPT&gt;
  function addMore() {
      $("&lt;DIV&gt;").load("sub.php", function() {
              $("#product").append($(this).html());
      }); 
  }
  function deleteRow() {
      $('DIV.product-item').each(function(index, item){
          jQuery(':checkbox', this).each(function () {
              if ($(this).is(':checked')) {
                  $(item).remove();
              }
          });
      });
  }
  &lt;/SCRIPT&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>file name:sub.php</p>
  
  <pre><code>&lt;DIV class="product-item float-clear" style="clear:both;"&gt;
  &lt;DIV class="float-left"&gt;&lt;input type="checkbox" name="item_index[]" /&gt;&lt;/DIV&gt;
  &lt;table cellspacing="2"&gt; 
   &lt;tr&gt;
  
  &lt;td&gt;&lt;DIV class="float-left"&gt;&lt;input type="text" name="name1[]" style="width:60px"/&gt;&lt;/DIV&gt;&lt;/td&gt;
  &lt;td&gt;&lt;DIV class="float-left"&gt;&lt;input type="text" name="name2[]" style="width:90px"/&gt;&lt;/DIV&gt;&lt;/td&gt;
  &lt;td&gt;&lt;DIV class="float-left"&gt;&lt;input type="text" name="name3[]" style="width:62px"/&gt;&lt;/DIV&gt;&lt;/td&gt;
  &lt;td&gt;&lt;DIV class="float-left"&gt;&lt;input type="text" name="name4[]" style="width:130px"/&gt;&lt;/DIV&gt;&lt;/td&gt;
  &lt;td&gt;&lt;DIV class="float-left"&gt;&lt;input type="text" name="name5[]" style="width:178px"/&gt;&lt;/DIV&gt;&lt;/td&gt;
  &lt;/DIV&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>here's my php code for inserting into database</p>
  
  <pre><code>&lt;?php
  $username="root";
  $password="sha12345";
  $hostname="localhost";
  
  $dbhandle = mysql_connect($hostname,$username,$password) or die("Could not connect to database");
  
  $select = mysql_select_db("sha", $dbhandle);
  
  
      if(isset($_POST['save']))
  {
      $personname = $_POST['first_name1'];
      $fathername = $_POST['first_name2'];
      $mothernname = $_POST['first_name3'];
  
  
      mysql_query("INSERT INTO sha(Name_person,Name_father,Name_mother)   VALUES ('$personname','$fathername','$mothernname')");  
  
  
  
          if(!empty($_POST["save"])) {
  
  
          $itemCount = count($_POST["name1"]);
          $itemValues=0;
          $query = "INSERT INTO sha (no_members,name_members,age_members,relation_members,occupation_members) VALUES ";
          $queryValue = "";
          for($i=0;$i&lt;$itemCount;$i++) {
              if(!empty($_POST["name1"][$i]) || !empty($_POST["name2"][$i]) || !empty($_POST["name3"][$i]) || !empty($_POST["name4"][$i]) || !empty($_POST["name5"][$i])) {
                  $itemValues++;
                  if($queryValue!="") {
                      $queryValue .= ",";
                  }
                  $queryValue .= "('" . $_POST["name1"][$i] . "', '" . $_POST["name2"][$i] . "', '" . $_POST["name3"][$i] . "', '" . $_POST["name4"][$i] . "', '" . $_POST["name5"][$i] . "')";
              }
          }
          $sql = $query.$queryValue;
          if($itemValues!=0) {
              $result = mysql_query($sql);
              if(!empty($result)) $message = "Added Successfully.";
          }
      }
      }
      mysql_close();
  
  ?&gt;
  </code></pre>