title: Multiple image upload with PHP saving only one file path to MySQL Database
body: <p>I am building a site for a friend and have set up a form that will upload multiple images to the database. It will only be accessed by one person, and is not visible to the general public so for sake of easy coding I have ignored a lot of possible threats to the code in order to just get it working.</p>
  
  <p>The HTML is simple it's just:</p>
  
  <pre><code>&lt;form action="(url here)" method="post" enctype="multipart/form-data"&gt;
       &lt;input type="file" name="files[]" multiple&gt;
       &lt;input id="formbutton" type= "submit" value="Upload!"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Here is the php that I'm worried about.</p>
  
  <pre><code>if(isset($_FILES['files'])){ 
      foreach ($_FILES['files']['tmp_name'] as $key =&gt; $tmp_name){
          $target="images/costume/";
          $target=$target.$_FILES['files']['name'][$key];
              if(move_uploaded_file($tmp_name, $target)){
                  $id = mysql_insert_id($con);
                  mysql_query("INSERT INTO costumes (id,name) VALUES ('$id','$name')");
              }
      }
  }
  </code></pre>
  
  <p>All of the image files upload to the directory on the server, but only ONE of the images actually stores into the MySQL table as a row with ID and file name.</p>
  
  <p>I've been racking my brain and searching like mad for a solution, but there aren't any that have worked. I know it has something to do with the foreach loop but I'm at a loss of what to do to make every image I upload create a row in the database.</p>
  
  <p>Thanks for the help guys.</p>
  
  <p>Sorry, I was making changes and forgot to switch back to the original code for the last section:</p>
  
  <pre><code>if(isset($_FILES['files'])){ 
      foreach ($_FILES['files']['tmp_name'] as $key =&gt; $tmp_name){
          $target="images/costume/";
          $target=$target.$_FILES['files']['name'][$key];
              if(move_uploaded_file($tmp_name, $target)){
                  $id = mysql_insert_id($con);
                  mysql_query("INSERT INTO costumes (id,name) VALUES ('$id','$target')");
              }
      }
  }
  </code></pre>
  
  <p>That's how it should be.</p>