title: php add data improperly into mysql
body: <p>I added the data to mysql but its not true. There are a few lines manually duplicating data. Add and commands my update it still works.I don't understand why the data added to the unsatisfactory. Who can help me not? thanks</p>
  
  <p>this is code html:</p>
  
  <pre><code> &lt;form id="myform" method="post"&gt;
  
      &lt;div&gt;
  
          &lt;div&gt;
              &lt;div&gt;
                  &lt;!--dongggggggggggggggggg 2--&gt;
                  &lt;?php
                  $select = mysql_query("Select * from Salary where code='C'");
                  $resultmax = mysql_query("SELECT MAX(stt) AS max_page FROM Salary where code='C'");
                  $rowmax = mysql_fetch_array($resultmax);
                  while ($resultRow = mysql_fetch_array($select)) {
                      ?&gt;
  
                      &lt;div class="row"&gt;
                          &lt;div class="col-lg-12"&gt;
                              &lt;input type="text" name="stt[]"
                                     value="&lt;?php echo $resultRow['stt'] ?&gt;"
                                     style="float:left; width:20px; border:none"
                                     readonly/&gt;
                              &lt;input type="hidden" name="d[]" value="C"
                                     style="float:left; width:20px; border:none"/&gt;
                              &lt;input id="dd" name="a[]" class="col-lg-35 a"
                                     value="&lt;?php echo $resultRow['col_a'] ?&gt;"
                                     type="text"&gt;
                              &lt;input name="b[]" class="col-lg-35 a nhay"
                                     value="&lt;?php echo $resultRow['col_b'] ?&gt;"
                                     onblur="luu()" type="text"&gt;
                              &lt;input name="c[]" class="col-lg-35 a"
                                     value="&lt;?php echo $resultRow['col_c'] ?&gt;"
                                     onblur="luu()" type="text"&gt;
                              &lt;input name="check_save" class="col-lg-1" type="checkbox"
                                     value="" onclick="return false;"&gt;
                          &lt;/div&gt;
  
                      &lt;/div&gt;
                  &lt;?php } ?&gt;
                  &lt;div class="row"&gt;
                      &lt;div class="col-lg-12"&gt;
                          &lt;input type="hidden" name="d[]" value="C"
                                 style="float:left; width:20px; border:none"/&gt;
                          &lt;input name="stt[]"
                                 value="&lt;?php echo $rowmax["max_page"] + 1 ?&gt;"
                                 style="float:left; width:20px;border:none" readonly/&gt;
                          &lt;input id="dd" name="a[]" class="col-lg-35 a" value=""
                                 type="text"&gt;
                          &lt;input name="b[]" class="col-lg-35 a"
                                 onblur="luu()" value="" type="text"&gt;
                          &lt;input name="c[]" class="col-lg-35 a"
                                 onblur="luu()" value="" type="text"&gt;
                          &lt;input id="save" name="check_save" class="col-lg-1 ab"
                                 type="checkbox" value="" onclick="return false;"&gt;
  
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="row" id="c"&gt;
                      &lt;div class="col-lg-12"&gt;
                          &lt;input type="hidden" name="d[]" value="C"
                                 style="float:left; width:20px; border:none"/&gt;
                          &lt;input name="stt[]" id="stt"
                                 value="&lt;?php echo $rowmax["max_page"] + 2 ?&gt;"
                                 style="float:left; width:20px;border:none" readonly/&gt;
                          &lt;input id="dd" name="a[]" class="col-lg-35 a" value=""
                                 type="text" &gt;
                          &lt;input name="b[]" class="col-lg-35 a nhay"
                                 onblur="luu()" value="" type="text"&gt;
                          &lt;input name="c[]" class="col-lg-35 a"
                                 onblur="luu()" value="" type="text"&gt;
                          &lt;input id="save" name="check_save" class="col-lg-1 ab"
                                 type="checkbox" value="" onclick="return false;"&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
  
                  &lt;!--dongggggggggggggggggg 1--&gt;
                  &lt;br&gt;
  
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div&gt;
          &lt;di&gt;
              &lt;div&gt;
                  &lt;!--dongggggggggggggggggg 1--&gt;
                  &lt;?php
                  $select = mysql_query("Select * from Salary where code='P'");
                  $resultmax = mysql_query("SELECT MAX(stt) AS max_page FROM Salary where code='P'");
                  $rowmax = mysql_fetch_array($resultmax);
                  while ($resultRow = mysql_fetch_array($select)) {
                      ?&gt;
                      &lt;!--dongggggggggggggggggg 1--&gt;
                      &lt;!--dongggggggggggggggggg 2--&gt;
                      &lt;div class="row"&gt;
                          &lt;div class="col-lg-12"&gt;
                              &lt;input type="text" name="stt[]"
                                     value="&lt;?php echo $resultRow['stt'] ?&gt;"
                                     style="float:left; width:20px; border:none"
                                     readonly/&gt;
                              &lt;input type="hidden" name="d[]" value="P"
                                     style="float:left; width:20px; border:none"/&gt;
  
                              &lt;input id="dd" name="a[]" class="col-lg-35 a"
                                     value="&lt;?php echo $resultRow['col_a'] ?&gt;"
                                     type="text"&gt;
                              &lt;input name="b[]" class="col-lg-35 a nhay"
                                     value="&lt;?php echo $resultRow['col_b'] ?&gt;"
                                     onblur="luu()" type="text"&gt;
                              &lt;input name="c[]" class="col-lg-35 a"
                                     value="&lt;?php echo $resultRow['col_c'] ?&gt;"
                                     onblur="luu()" type="text"&gt;
                              &lt;input name="check_save" class="col-lg-1" type="checkbox"
                                     value="" onclick="return false;"&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;?php } ?&gt;
  
                  &lt;div class="row"&gt;
                      &lt;div class="col-lg-12"&gt;
                          &lt;input type="hidden" name="d[]" value="P"
                                 style="float:left; width:20px; border:none"/&gt;
                          &lt;input name="stt[]"
                                 value="&lt;?php echo $rowmax["max_page"] + 1 ?&gt;"
                                 style="float:left; width:20px;border:none" readonly/&gt;
                          &lt;input name="a[]" class="col-lg-35 a" value="" type="text"&gt;
                          &lt;input name="b[]" class="col-lg-35 a" value="" onblur="luu()" type="text"&gt;
                          &lt;input name="c[]" class="col-lg-35 a" value=""
                                 onblur="luu()" type="text"&gt;
                          &lt;input id="save" name="check_save" class="col-lg-1 ab" type="checkbox" value="" onclick="return false;"&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;!--dongggggggggggggggggg 1--&gt;
                  &lt;!--dongggggggggggggggggg 2--&gt;
                  &lt;div class="row" id="d"&gt;
                      &lt;div class="col-lg-12"&gt;
                          &lt;input type="hidden" name="d[]" value="P" style="float:left; width:20px; border:none"/&gt;
                          &lt;input name="stt[]" value="&lt;?php echo $rowmax["max_page"] + 2 ?&gt;" style="float:left; width:20px;border:none" readonly/&gt;
                          &lt;input id="dd" name="a[]" class="col-lg-35 a" value="" type="text"&gt;
                          &lt;input name="b[]" class="col-lg-35 a nhay" value="" onblur="luu()" type="text"&gt;
                          &lt;input name="c[]" class="col-lg-35 a" value="" onblur="luu()" type="text"&gt;
                          &lt;input id="save" name="check_save" class="col-lg-1 ab" type="checkbox" value="" onclick="return false;"&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;!--dongggggggggggggggggg 1--&gt;
                  &lt;br&gt;
  
  
              &lt;/div&gt;
          &lt;/di&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>this is javascript :</p>
  
  <pre><code>&lt;script type="text/javascript" language="javascript"&gt;
  
  function luu() {
      $.ajax({
          type: 'POST',
          url: 'insert.php',
          data: $('#myform').serialize(),
          dataType: 'JSON',
          success: function (data) {
              check();
          }
      });
  }
  $(document).ready(function () {
      var currentBoxNumber = 0;
      $(document).on('keyup', '.a', function (e) {
          if (e.keyCode == 13) {
              textboxes = $("input.a");
              currentBoxNumber = textboxes.index(this);
              if (textboxes[currentBoxNumber + 1] != null) {
                  nextBox = textboxes[currentBoxNumber + 1];
                  if (nextBox.id == "dd") {
                      luu();
                  }
  
                  nextBox.focus();
                  return false;
              }
          }
      });
      var i = document.getElementById("stt").value;
      $(document).on('keyup', '.nhay', function (e) {
          if (e.keyCode == 13) {
              $('.a').removeClass('nhay');
              var y = parseInt(i) + parseInt(1);
              $('#c').append('&lt;div&gt;&lt;input type="hidden" name="d[]" value="C" style="float:left; width:20px; border:none"/&gt;&lt;input name="stt[]" value="' + y + '" style="float:left; width:20px;border:none" readonly/&gt;&lt;input id="dd" name="a[]" class="col-lg-35 a" value="" type="text"&gt;&lt;input name="b[]" onblur="luu()" class="col-lg-35 a nhay" value="" type="text"&gt;&lt;input name="c[]" onblur="luu()" class="col-lg-35 a" value="" type="text" &gt;&lt;input id="save" name="check_save" class="col-lg-1 ab" type="checkbox" value="" onclick="return false;"&gt;&lt;/div&gt;');
              $('#d').append('&lt;div&gt;&lt;input type="hidden" name="d[]" value="P" style="float:left; width:20px; border:none"/&gt;&lt;input name="stt[]" value="' + y + '" style="float:left; width:20px;border:none" readonly/&gt;&lt;input id="dd" name="a[]" class="col-lg-35 a" value="" type="text"&gt;&lt;input name="b[]" onblur="luu()" class="col-lg-35 a nhay" value="" type="text"&gt;&lt;input name="c[]" onblur="luu()" class="col-lg-35 a" value="" type="text" &gt;&lt;input name="check_save" class="col-lg-1 ab" type="checkbox" value="" onclick="return false;"&gt;&lt;/div&gt;');
              i++;
          }
  
      });
  });
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;script&gt;
  function check() {
      var myForm = document.forms.myform;
      var myControls = $(myForm).find('input[name="a[]"]').filter(function () {
          if (!$.trim(this.value)) {
              $(this).siblings('.ab').prop('checked', false)
          } else {
              $(this).siblings('.ab').prop('checked', true)
          }
      });
  
  
  }
  </code></pre>
  
  <p></p>
  
  <p>this is insert.php :</p>
  
  <pre><code>$a=$_POST['a'];
  $b=$_POST['b'];
  $c=$_POST['c'];
  $sott=$_POST['stt'];
  $d=$_POST['d'];
  $index=0;
  
  foreach($a as $s){
  $check="Select ID from Salary where stt='".$sott[$index]."' and code='".$d[$index]."'";
  $result1 = mysql_query($check);
  $myid = mysql_num_rows($result1);
  if ($myid==null)
  {
      $sql = "INSERT INTO Salary(code,col_a,col_b,col_c,stt) VALUES('".$d[$index]."','$s','".$b[$index]."','".$c[$index]."','".$sott[$index]."')";
  
      $result = mysql_query($sql);
  }
  else
  {
      $sql="Update Salary set col_a='$s',col_b='".$b[$index]."',col_c='".$c[$index]."' where stt='".$sott[$index]."' and code='".$d[$index]."'";
      $result = mysql_query($sql);
  
  }
  $index++;
  
   }
   if ($result) { echo "1";}
  </code></pre>
  
  <p>this is output :
  <a href="https://i.stack.imgur.com/vyH8k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vyH8k.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/Wndnw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Wndnw.png" alt="enter image description here"></a></p>