title: Creating a simple PHP Image Upload within a Form
body: <p>I am working on a simple Image upload form, with a place for people to add comments and other details. So far i have reached this code. </p>
  
  <p>Problem: When i run this, the file which i upload gets to the directory fine, but the data in the form isn't written to the database. </p>
  
  <pre><code>&lt;?php
  //$con = mysql_connect("localhost","root","");
  $con = mysql_connect("localhost","Login","password","DBname");
  if (!$con)
  {
      die('Could not connect: ' . mysql_error());
  }
  
  
  if(isset($_FILES['files'])){
      $errors= array();
      foreach($_FILES['files']['tmp_name'] as $key =&gt; $tmp_name ){
          $file_fname=$_POST['fname'][$key];
          $file_Lname=$_POST['lanem'][$key];
          $file_email=$_POST['email'][$key];
          $file_school=$_POST['school'][$key];
          $file_comment=$_POST['comment'][$key];
          $file_name = $key.$_FILES['files']['name'][$key];
          $file_tmp =$_FILES['files']['tmp_name'][$key];
  
  
          if($file_size &gt; 2097152){
              $errors[]='File size must be less than 2 MB';
          }
  
  
  
          $query = "INSERT INTO sameplTable(`fname`,`lname`,`email`,`school`,`comment`,`filename`) VALUES
          ('$fname','$lname','$email','$school','$comment','$filename')";
  
  
  
  
          $desired_dir="user_data";
          if(empty($errors)==true){
              if(is_dir($desired_dir)==false){
                  mkdir("$desired_dir", 0700);        // Create directory if it does not exist
              }
              if(is_dir("$desired_dir/".$file_name)==false){
                  move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
              }else{                                  // rename the file if another one exist
                  $new_dir="$desired_dir/".$file_name.time();
                  rename($file_tmp,$new_dir) ;
              }
              mysql_query($query);
          }else{
              print_r($errors);
          }
      }
      if(empty($error)){
          echo "Success";
      }
  }
  ?&gt;
  
  
  &lt;form action="" method="POST" enctype="multipart/form-data"&gt;
      &lt;input type="file" name="files[]" multiple/&gt;
      Value1: &lt;input type="text" name="fname"&gt;&lt;br&gt;
      Value2: &lt;input type="text" name="Lname"&gt;&lt;br&gt;
      Value3: &lt;input type="text" name="email"&gt;&lt;br&gt;
      Value4: &lt;input type="text" name="school"&gt;&lt;br&gt;
      Value5: &lt;input type="text" name="comment"&gt;&lt;br&gt;
  
      &lt;input type="Submit"&gt;
  &lt;/form&gt;
  </code></pre>