title: Saving multiple form data to mysql using php
body: <p>I have a table form which has a add new row button which upon clicked adds a new row to the table. I am trying to save all the rows in MySQL on clicking save button.</p>
  
  <p>The code I wrote saves only one row no matter how many row I add. Could someone please tell my what am I doing wrong.</p>
  
  <p>I searched Google but couldn't get anywhere.</p>
  
  <p>Here are my codes:</p>
  
  <p>save.php</p>
  
  <pre><code>&lt;?php
  include('connection.php');
  
  if(isset($_POST['submit'])){
  
      $row_data = array();
      foreach($_POST['category'] as $row=&gt;$category){
          $category = mysql_real_escape_string($category);
          $itemName = mysql_real_escape_string($_POST['itemName'][$row]);
          $brand = mysql_real_escape_string($_POST['brand'][$row]);
          $model = mysql_real_escape_string($_POST['model'][$row]);
          $sellingPrice = mysql_real_escape_string($_POST['sellingPrice'][$row]);
  
          $row_data[] = "('$category','$itemName','$brand','$model','$sellingPrice')";
      }
  }
  if(!empty($row_data)){
      $insert_query = mysql_query("INSERT INTO sale(Category,ItemName,Brand,Model,SellingPrice) VALUES".implode(',', $row_data));
  
      if(!$insert_query){
          echo "Error: " . mysql_error();
      }else{
          echo "Data Saved Successfully";
      }
  }
  ?&gt;
  </code></pre>
  
  <p>and this is my html form</p>
  
  <pre><code>&lt;form name="form1" id="myForm" action="saveSale.php" method="post"&gt;
                      &lt;tr class="cloneme"&gt;
                          &lt;td&gt;&lt;input type="text" name="category[]"&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;input type="text" name="itemName[]"&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;input type="text" name="brand[]"&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;input type="text" name="model[]"&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;input type="text" name="sellingPrice[]"&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/tbody&gt;
              &lt;/table&gt;
          &lt;/div&gt;
          &lt;div class="eventButtons"&gt;
              &lt;input type="submit" name="submit" id="submit" value="Save"&gt;
              &lt;input type="reset" name="reset" id="reset" value="Clear"  class="btn"&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  </code></pre>