title: Dynamic input and add it into a database
body: <p>Hello everybody, likle my title tells I'm making a dynamic input but I have errors and I'm going to cry haha. Seriously I have difficulties to make it. I want to add dynamic input and add value in my database. Here is my code :</p>
  
  <pre><code>&lt;?php
  // Connect to the DB
  $link = mysqli_connect("localhost","root","","testlp") or die("Error " . mysqli_error($link));
  
  // store in the DB 
  if(!empty($_POST['ok'])) {  
      // first delete the records marked for deletion. Why? Because we don't want to process them in the code below
      if( !empty($_POST['delete_ids']) and is_array($_POST['delete_ids'])) {
          // you can optimize below into a single query, but let's keep it simple and clear for now:
          foreach($_POST['delete_ids'] as $id) {
              $sql = "DELETE FROM recherche WHERE id=$id";
              $link-&gt;query($sql);
          }
      }
  
  
  
      // adding new recherche
      if(!empty($_POST['name'])) {
      //  ( $i = 0; $i &lt; count($_POST['name']); $i++)
          {
              $sql = "INSERT INTO recherche (name) VALUES ".$_POST['name'][$i];
              $link-&gt;query($sql);
          }
      }   
  }
  
  // select existing recherche here
  $sql="SELECT * FROM recherche ORDER BY id";
  $result = $link-&gt;query($sql);
  ?&gt;
  
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Simple example of dynamically adding rows with jQuery&lt;/title&gt;
      &lt;script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;div style="width:90%;margin:auto;"&gt;
      &lt;h1&gt;Simple example of dynamically adding rows with jQuery&lt;/h1&gt;
  
      &lt;form method="post"&gt;
      &lt;div id="itemRows"&gt;
  
       Item name: &lt;input type="text" name="add_name" /&gt; &lt;input onclick="addRow(this.form);" type="button" value="Add row" /&gt; (This row will not be saved unless you click on "Add row" first)
  
      &lt;?php
      // let's assume you have the product data from the DB in variable called $recherche
      while($product = mysqli_fetch_array($result)): ?&gt;
          &lt;p id="oldRow&lt;?=$product['id']?&gt;"&gt; Item name: &lt;input type="text" name="name&lt;?=$product['id']?&gt;" value="&lt;?=$product['name']?&gt;" /&gt; &lt;input type="checkbox" name="delete_ids[]" value="&lt;?=$product['id']?&gt;"&gt; Mark to delete&lt;/p&gt;
      &lt;?php endwhile;?&gt;
  
      &lt;/div&gt;
  
      &lt;p&gt;&lt;input type="submit" name="ok" value="Save Changes"&gt;&lt;/p&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  
  &lt;script type="text/javascript"&gt;
  var rowNum = 0;
  function addRow(frm) {
      rowNum ++;
      var row = '&lt;p id="rowNum'+rowNum+'"&gt;Item name: &lt;input type="text" name="name[]" value="'+frm.add_name.value+'"&gt; &lt;input type="button" value="Remove" onclick="removeRow('+rowNum+');"&gt;&lt;/p&gt;';
      jQuery('#itemRows').append(row);
      frm.add_qty.value = '';
      frm.add_name.value = '';
  }
  
  function removeRow(rnum) {
      jQuery('#rowNum'+rnum).remove();
  }
  &lt;/script&gt;
  &lt;/body&gt; 
  &lt;/html&gt;
  </code></pre>
  
  <p>I've problem in my loop and I get this error :</p>
  
  <blockquote>
    <p>Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result,
    boolean given in C:\wamp\www\testing\dynamic-form-fields.html.php on
    line 51 This is the line 51 in the previous code</p>
  </blockquote>
  
  <pre><code>while($product = mysqli_fetch_array($result)): ?&gt;
          &lt;p id="oldRow&lt;?=$product['id']?&gt;"&gt; Item name: &lt;input type="text" name="name&lt;?=$product['id']?&gt;" value="&lt;?=$product['name']?&gt;" /&gt; &lt;input type="checkbox" name="delete_ids[]" value="&lt;?=$product['id']?&gt;"&gt; Mark to delete&lt;/p&gt;
      &lt;?php endwhile;?&gt;
  </code></pre>
  
  <p>Thanks for support!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here the new part of the code you guys helped me with : but nothing happens when I submit to the database. I checked in phpmyadmin of my database.</p>
  
  <pre><code>&lt;?php
  // Connect to the DB
  $link = mysqli_connect("localhost","root","","testlp") or die("Error " . mysqli_error($link));
  
  // store in the DB 
  if(!empty($_POST['ok'])) {  
      // first delete the records marked for deletion. Why? Because we don't want to process them in the code below
      if( !empty($_POST['delete_ids']) and is_array($_POST['delete_ids'])) {
  
          foreach($_POST['delete_ids'] as $id) {
              $sql = "DELETE FROM recherche WHERE id=$id";
              $link-&gt;query($sql);
          }
      }
  
      // adding new recherche
      if(!empty($_POST['name'])) {
      foreach($_POST['name'] as $name)
      {
          //escape special characters from inputed "name" to prevent SQL injection.
          $sql = "INSERT INTO recherche (name) VALUES ".mysqli_real_escape_string($link,$name);
          $link-&gt;query($sql);
      }
  } 
  }
  
  // select existing recherche here
  $sql="SELECT * FROM recherche ORDER BY id";
  $result = $link-&gt;query($sql);
  ?&gt;
  
  &lt;html&gt;
  &lt;head&gt;
      &lt;script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;div style="width:90%;margin:auto;"&gt;
  
  
      &lt;form method="post"&gt;
      &lt;div id="itemRows"&gt;
  
       Item name: &lt;input type="text" name="add_name" /&gt; &lt;input onclick="addRow(this.form);" type="button" value="Add row" /&gt; (This row will not be saved unless you click on "Add row" first)
       &lt;?php
  if($result!=false &amp;&amp; mysqli_num_rows($result)&gt;0){
      while($product = mysqli_fetch_array($result)): ?&gt;
          &lt;p id="oldRow&lt;?=$product['id']?&gt;"&gt; Item name: &lt;input type="text" name="name&lt;?=$product['id']?&gt;" value="&lt;?=$product['name']?&gt;" /&gt; &lt;input type="checkbox" name="delete_ids[]" value="&lt;?=$product['id']?&gt;"&gt; Mark to delete&lt;/p&gt;
      &lt;?php endwhile;
  
  }
  ?&gt;
      &lt;/div&gt;
  
      &lt;p&gt;&lt;input type="submit" name="ok" value="Save Changes"&gt;&lt;/p&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  
  &lt;script type="text/javascript"&gt;
  var rowNum = 0;
  function addRow(frm) {
      rowNum ++;
      var row = '&lt;p id="rowNum'+rowNum+'"&gt;Item name: &lt;input type="text" name="name[]" value="'+frm.add_name.value+'"&gt; &lt;input type="button" value="Remove" onclick="removeRow('+rowNum+');"&gt;&lt;/p&gt;';
      jQuery('#itemRows').append(row);
      frm.add_qty.value = '';
      frm.add_name.value = '';
  }
  
  function removeRow(rnum) {
      jQuery('#rowNum'+rnum).remove();
  }
  &lt;/script&gt;
  &lt;/body&gt; 
  &lt;/html&gt;
  </code></pre>
  
  <p>Here finaly are the result with great guys in this forum !</p>
  
  <p>Feel free to edit or make whatever you want with this code! </p>
  
  <pre><code>&lt;?php
  // Connect to the DB
  $link = mysqli_connect("localhost","root","","testlp") or die("Error " . mysqli_error($link));
  
  // store in the DB 
  if(!empty($_POST['ok'])) {  
      // first delete the records marked for deletion. Why? Because we don't want to process them in the code below
      if( !empty($_POST['delete_ids']) and is_array($_POST['delete_ids'])) {
  
          foreach($_POST['delete_ids'] as $id) {
              $sql = "DELETE FROM recherche WHERE id=$id";
              $link-&gt;query($sql);
          }
      }
  
      // adding new recherche
      if(!empty($_POST['name'])) {
      foreach($_POST['name'] as $name)
      {
          //escape special characters from inputed "name" to prevent SQL injection.
  
          $sql = "INSERT INTO recherche (name) VALUES ('".mysqli_real_escape_string($link,$name)."')";
          $link-&gt;query($sql);
      }
  } 
  }
  
  // select existing recherche here
  $sql="SELECT * FROM recherche ORDER BY id";
  $result = $link-&gt;query($sql);
  ?&gt;
  
  &lt;html&gt;
  &lt;head&gt;
      &lt;script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;div style="width:90%;margin:auto;"&gt;
  
  
      &lt;form method="post"&gt;
      &lt;div id="itemRows"&gt;
  
       Item name: &lt;input type="text" name="add_name" /&gt; &lt;input onclick="addRow(this.form);" type="button" value="Add row" /&gt; (This row will not be saved unless you click on "Add row" first)
       &lt;?php
  if($result!=false &amp;&amp; mysqli_num_rows($result)&gt;0){
      while($product = mysqli_fetch_array($result)): ?&gt;
          &lt;p id="oldRow&lt;?=$product['id']?&gt;"&gt; Item name: &lt;input type="text" name="name&lt;?=$product['id']?&gt;" value="&lt;?=$product['name']?&gt;" /&gt; &lt;input type="checkbox" name="delete_ids[]" value="&lt;?=$product['id']?&gt;"&gt; Mark to delete&lt;/p&gt;
      &lt;?php endwhile;
  
  }
  ?&gt;
      &lt;/div&gt;
  
      &lt;p&gt;&lt;input type="submit" name="ok" value="Save Changes"&gt;&lt;/p&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  
  &lt;script type="text/javascript"&gt;
  var rowNum = 0;
  function addRow(frm) {
      rowNum ++;
      var row = '&lt;p id="rowNum'+rowNum+'"&gt;Item name: &lt;input type="text" name="name[]" value="'+frm.add_name.value+'"&gt; &lt;input type="button" value="Remove" onclick="removeRow('+rowNum+');"&gt;&lt;/p&gt;';
      jQuery('#itemRows').append(row);
      frm.add_qty.value = '';
      frm.add_name.value = '';
  }
  
  function removeRow(rnum) {
      jQuery('#rowNum'+rnum).remove();
  }
  &lt;/script&gt;
  &lt;/body&gt; 
  &lt;/html&gt;
  </code></pre>