title: How to upload files with a dynamic form and save path/filename to mysql together with other data using PHP
body: <p>I have a dynamic form,that looks pretty much like this:
  <a href="https://i.stack.imgur.com/Ap6lo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ap6lo.png" alt="Dynamic form drawing"></a></p>
  
  <p>The idea is to add a new row every time I click "Add row" ( usign JQuery here) and each new row should contain a "Browse" button, allowing to upload a file for each added user newly added. The remove button simply removes unneeded rows.</p>
  
  <p>I'm using PHP and MySQL to store the input fields data and file path.</p>
  
  <p>My problem is that I cannot figure out how to implement the "upload file" correctly and also how to add the path/filename to the php/mysql query.</p>
  
  <p>My code, so far, looks like:</p>
  
  <pre><code>&lt;?php
  $mysql_db_hostname = "localhost";
  $mysql_db_user = "root";
  $mysql_db_password = "password";
  $mysql_db_database = "dynamic";
  
  $dbc = mysql_connect($mysql_db_hostname, $mysql_db_user, 
  
  $mysql_db_password) or die("Could not connect database");
  mysql_select_db($mysql_db_database, $dbc) or die("Could not select database");
  ?&gt;
  
  
  &lt;?
  if (isset($_POST['add_account'])) { 
      if ($_POST['fields']) {
          foreach( $_POST['fields'] as $key=&gt;$fieldArray ) {                          
             // new code added from here
              if (!empty($_FILES)) {
  
                  $uploaddir = 'upload/';
                  $uploadfile = $uploaddir . basename($_FILES['userfiles']['name']);
  
  
                  if (move_uploaded_file($_FILES['userfiles']['tmp_name'], $uploadfile)) {
                      echo "File is valid, and was successfully uploaded.&lt;br /&gt;";
                  } else {
                      echo "File not uploaded!&lt;br /&gt;";
                  }               
  
              }
              // new code added until here
  
              $keys = array_keys($fieldArray);
              $values = array_map("mysql_real_escape_string",$fieldArray);                
              $q = "INSERT INTO accounts (".implode(',',$keys).", file_uploaded) VALUES ('".implode('\',\'',$values)."', ".$uploadfile." )";
              $r = mysql_query($q, $dbc );                                            
  
          }
      }
      echo "&lt;i&gt;&lt;h2&gt;&lt;strong&gt;" . count($_POST['fields']) . "&lt;/strong&gt; Account(s) Added&lt;/h2&gt;&lt;/i&gt;";       
  }
  ?&gt;
  
  &lt;?php if (!isset($_POST['add_account'])) { ?&gt; 
  
  &lt;form method="post" action="" enctype="multipart/form-data"&gt;
  
  &lt;p id="add_field"&gt;&lt;a class="btn btn-default" href="#"&gt;Add Rows&lt;/a&gt;&lt;/p&gt;
  &lt;table id="myTable"&gt;
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;#&lt;/th&gt;
          &lt;th&gt;First Name:&lt;/th&gt;
          &lt;th&gt;Last Name:&lt;/th&gt;
          &lt;th&gt;E-mail:&lt;/th&gt;
          &lt;th&gt;Upload file&lt;/th&gt;            
          &lt;th&gt;&lt;/th&gt;           
      &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody id="container"&gt;
  &lt;/tbody&gt;
  &lt;/table&gt;
  
  &lt;input class="btn btn-default" type="submit" name="add_account"  value="Submit"  /&gt;
  &lt;/form&gt;
  &lt;?php } ?&gt;
  
  &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  
      $(function(){
          var counter = 0;
          $('p#add_field').click(function(){
              counter += 1;
              $('#container').append(
              '&lt;tr&gt;&lt;td&gt;' + counter + '&lt;/td&gt;&lt;td&gt;&lt;input name="fields['+counter+'][first]" type="text"  placeholder="First Name" required/&gt;&lt;/td&gt;&lt;td&gt;&lt;input name="fields['+counter+'][last]" type="text"  placeholder="Last Name" required/&gt;&lt;/td&gt;&lt;td&gt;&lt;input name="fields['+counter+'][email]" type="email"  placeholder="email" required/&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="userfile" name="fields['+counter+'][file_uploaded][]" type="file" /&gt;&lt;/td&gt;&lt;td&gt;&lt;input button" value="Remove" onclick="delRow(this)"&gt;&lt;/td&gt;&lt;/tr&gt;');
  
          });
      });
  
      function delRow(currElement) {
           var parentRowIndex = currElement.parentNode.parentNode.rowIndex;
           document.getElementById("myTable").deleteRow(parentRowIndex);
      }
  
  &lt;/script&gt;
  </code></pre>
  
  <p>If I try to echo <code>$uploadfile</code> I get no result... </p>
  
  <p>The  result of the <code>print_r($_FILES)</code> is:</p>
  
  <pre><code>    Array
  (
      [fields] =&gt; Array
          (
              [name] =&gt; Array
                  (
                      [1] =&gt; Array
                          (
                              [file_uploaded] =&gt; Array
                                  (
                                      [0] =&gt; a4.jpg
                                  )
  
                          )
  
                  )
  
              [type] =&gt; Array
                  (
                      [1] =&gt; Array
                          (
                              [file_uploaded] =&gt; Array
                                  (
                                      [0] =&gt; image/jpeg
                                  )
  
                          )
  
                  )
  
              [tmp_name] =&gt; Array
                  (
                      [1] =&gt; Array
                          (
                              [file_uploaded] =&gt; Array
                                  (
                                      [0] =&gt; /tmp/phpIaNYYD
                                  )
  
                          )
  
                  )
  
              [error] =&gt; Array
                  (
                      [1] =&gt; Array
                          (
                              [file_uploaded] =&gt; Array
                                  (
                                      [0] =&gt; 0
                                  )
  
                          )
  
                  )
  
              [size] =&gt; Array
                  (
                      [1] =&gt; Array
                          (
                              [file_uploaded] =&gt; Array
                                  (
                                      [0] =&gt; 6063
                                  )
  
                          )
  
                  )
  
          )
  
  )
  </code></pre>
  
  <p>The mysql table (accounts) looks like:</p>
  
  <pre><code> id | first | last | email | file_uploaded 
  </code></pre>
  
  <p>The id field is set as Primary with Autoincrement , the rest are Varchar (300 ).</p>
  
  <p>I can't figure out how to upload files and save the path in mysql using this kind of dynamic form.</p>
  
  <p>Everything else works perfectly.</p>
  
  <p>Can someone, please, give me some code example and put me on the right path with the upload files issue for this specific case?</p>
  
  <p>Disclaimer: I know I should use PDO and mysqli. I'm learning PDO right now, but I'm yet far from knowing it, that's why I used old/classic mysql here.
  Also, most of the code is put together from various answers on similar issues found on this site.</p>