title: Arrays from multiple upload form, Upload images then insert to database (PHP, MySQL)
body: <p>Language: <strong>PHP</strong> / MySQL  </p>
  
  <p>I am going out of my mind, I really have to ask now... I have a multiple file upload form:  </p>
  
  <p><code>&lt;input type="file" name="fileupload[]" multiple&gt;</code>  </p>
  
  <p>With the help of some Javascript, on each change made to this input, it appends a list of filenames, + a formatted string (grabbed from the filename) inside another input, so onchange we have a <strong>layout as shown below</strong> (assuming that we just added some images):</p>
  
  <p><img src="https://i.stack.imgur.com/JRbmK.gif" alt="Multiple Upload Form">
  <strong>Almost similar to:</strong> <a href="http://jsfiddle.net/pxfunc/WWNnV/4/" rel="nofollow noreferrer">http://jsfiddle.net/pxfunc/WWNnV/4/</a></p>
  
  <hr>
  
  <p><strong>// An HTML representation of such layout would be...</strong> (assuming that we added 3 images)</p>
  
  <p><code>&lt;input type="file" name="fileupload[]" multiple&gt;</code> </p>
  
  <ul>
  <li>image-name-1.jpg  &nbsp; <code>&lt;input type="text" value="Image Name 1" name="keyword[]"&gt;</code></li>
  <li>justsome_file.png  &nbsp; <code>&lt;input type="text" value="Justsome File" name="keyword[]"&gt;</code></li>
  <li>some_Img-031.gif  &nbsp; <code>&lt;input type="text" value="Some Img 031" name="keyword[]"&gt;</code></li>
  </ul>
  
  <p><code>&lt;input type="submit" value="Upload"&gt;</code>  </p>
  
  <hr>
  
  <p><em>I have it this way because aside from uploading the files, <strong>I would also like to add them to my database, with a default title</strong> based on its filename (and the option to set/change this title for each image as I upload it). There is no problem with my form.</em></p>
  
  <p><strong>PROBLEM:</strong> My dilemma lies inside the PHP page where the form data/action is submitted.</p>
  
  <p>I can only manage to either:  </p>
  
  <ul>
  <li>Upload correct images, but get same title for all</li>
  <li>Insert correct titles, but get same image for all</li>
  </ul>
  
  <p><strong>Here is my PHP action page:</strong> (Currently uploading correct images, but having <strong>same title</strong> for all)  </p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  // CONNECT TO DATABASE...
  // INCLUDE UPLOAD CLASS LIBRARY
  include (dirname(__FILE__).'/lib/class.upload.php');
  
  
  $files = array();
  foreach ($_FILES['fileupload'] as $k =&gt; $l)
  {
      foreach ($l as $i =&gt; $v)
      {
          if (!array_key_exists($i, $files))
          $files[$i] = array();
          $files[$i][$k] = $v;
          $imagename = $_POST['keyword'][$i];
      }
  }
  
  // create an array here to hold file names
  $uploaded = array();
  foreach ($files as $file)
   {
              $generate_name = rand(100,99999); 
              $generate_name_extra = rand(200,9999);
              $filenamex = "COVER_PHOTO_".$generate_name.$generate_name_extra."_".time();
              $filenamex_thumb = $filenamex."_thumb";
  
              $handle = new upload($file);
              if ($handle-&gt;uploaded) {
              $this_upload = array();
  
              ///// 1 ////////////////////////////////////////////////////////////////////
              $handle-&gt;file_new_name_body   = $filenamex_thumb;
              $handle-&gt;file_force_extension = true;
              $handle-&gt;image_resize         = true;
              $handle-&gt;image_x              = '300';
              $handle-&gt;image_ratio_y        = true;
              $handle-&gt;jpeg_quality = '100';
  
              // ABSOLUTE PATH BELOW
              $handle-&gt;process($absoRoot.'covers/thumbs/');
              ////////////////////////////////////////////////////////////////////////////
              if ($handle-&gt;processed) {
  
        // store the image filename
      $this_upload['image'] = $handle-&gt;file_dst_name; // Destination file name
      $this_upload['body'] = $handle-&gt;file_dst_name_body; // Destination file name body
      $this_upload['extension'] = $handle-&gt;file_dst_name_ext; // Destination file extension
  
          $category_id = $_POST['cat'];
          $hiddenvalues = explode ("|",$_POST["cat"]);
          $category = $hiddenvalues[0];
          $category_name = $hiddenvalues[1];
  
  
                  $sql = 'INSERT INTO cover (id, img, keyword, category_name, cat_id) VALUES ("", "'.$this_upload['image'].'", "'.$imagename.'", "'.$category_name.'", "'.$category.'")';
                   mysql_query($sql);
          }
  
      $handle-&gt;clean();
          header("Location: ./upload.php");
                  $message = "";
      } else {
  
           echo '  file not uploaded to the wanted location';
           echo '  Error: ' . $handle-&gt;error . '';
  
        }
  } ?&gt;
  </code></pre>
  
  <p>(I use the <a href="http://www.verot.net/download/class.upload.php/class.upload.txt" rel="nofollow noreferrer"><strong>Upload Class</strong> by Colin Verot</a> to handle image uploads, and their FAQ tutorial to handle MULTIPLE image uploads on <a href="http://www.verot.net/php_class_upload_faq.htm?PHPSESSID=accbee5eb0a694348a445a61a4e40ba1" rel="nofollow noreferrer">this page, under: <strong>What about multiple uploads?</strong></a>)</p>
  
  <p><strong>This would work perfect if I were just uploading images, however I added the functionality of adding each image data to my database. &amp; This is where it gets confusing.</strong></p>
  
  <p>I'm sure the key is placing the SQL query inside the right foreach, or perhaps making another one, but I've tried that &amp; it only gives me 1 good result for either the image upload or the title, never for both.</p>
  
  <p>I need to upload the image to the site, then store its data (including image path) to my database.</p>
  
  <p>Please look into my code and enlighten me how to solve this problem? A snippet clue would really be great for now as I am already very confused after having tried all I could think of. Thank you so much!</p>