title: How to create multiple dynamic form fields and insert in mysql database
body: <p>I need to create dynamic text fields which contains: name surname age gender. I should have a button called "add new user" which simply adds a new row where a I can type in name surname age gender. I need it dynamic because I don't know how many users will be added per event. I also need to store name surname age gender (x) amount of added rows in database.</p>
  
  <p>I need something similar to below : however I need an additional 3 rows next to each other. So I will "INSERT INTO my_hobbies (name, surname, age, gender)</p>
  
  <p>I know this sounds far fetched but i am struggling with this.</p>
  
  <pre><code>&lt;?php 
  require("dbconn.php");
  ?&gt;
  
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;script type="text/javascript" src="jquery.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  var counter = 0;
  $(function(){
  $('p#add_field').click(function(){
  counter += 1;
  $('#container').append(
  '&lt;strong&gt;Hobby No. ' + counter + '&lt;/strong&gt;&lt;br /&gt;' 
  + '&lt;input id="field_' + counter + '" name="dynfields[]' + '" type="text" /&gt;&lt;br /&gt;' );
  
  });
  });
  &lt;/script&gt; 
  
  &lt;body&gt;
  
  &lt;?php
  if (isset($_POST['submit_val'])) {
  if ($_POST['dynfields']) {
  foreach ( $_POST['dynfields'] as $key=&gt;$value ) {
  $values = mysql_real_escape_string($value);
  $query = mysql_query("INSERT INTO my_hobbies (hobbies) VALUES ('$values')", $connection );  
  
  }
  }
  
  echo "&lt;i&gt;&lt;h2&gt;&lt;strong&gt;" . count($_POST['dynfields']) . "&lt;/strong&gt; Hobbies Added&lt;/h2&gt;&lt;/i&gt;";
  
  mysql_close();
  }
  ?&gt;
  &lt;?php if (!isset($_POST['submit_val'])) { ?&gt;
  &lt;h1&gt;Add your Hobbies&lt;/h1&gt;
  &lt;form method="post" action=""&gt;
  
  
  &lt;div id="container"&gt;
  &lt;p id="add_field"&gt;&lt;a href="#"&gt;&lt;span&gt;Click To Add Hobbies&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
  &lt;/div&gt;
  
  &lt;input type="submit" name="submit_val"  value="Submit"  /&gt;
  &lt;/form&gt;
  &lt;?php } ?&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>