Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8135245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:21:42+00:00 2026-06-06T10:21:42+00:00

Language: PHP / MySQL I am going out of my mind, I really have

  • 0

Language: PHP / MySQL

I am going out of my mind, I really have to ask now… I have a multiple file upload form:

<input type="file" name="fileupload[]" multiple>

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 layout as shown below (assuming that we just added some images):

Multiple Upload Form
Almost similar to: http://jsfiddle.net/pxfunc/WWNnV/4/


// An HTML representation of such layout would be… (assuming that we added 3 images)

<input type="file" name="fileupload[]" multiple>

  • image-name-1.jpg   <input type="text" value="Image Name 1" name="keyword[]">
  • justsome_file.png   <input type="text" value="Justsome File" name="keyword[]">
  • some_Img-031.gif   <input type="text" value="Some Img 031" name="keyword[]">

<input type="submit" value="Upload">


I have it this way because aside from uploading the files, I would also like to add them to my database, with a default title 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.

PROBLEM: My dilemma lies inside the PHP page where the form data/action is submitted.

I can only manage to either:

  • Upload correct images, but get same title for all
  • Insert correct titles, but get same image for all

Here is my PHP action page: (Currently uploading correct images, but having same title for all)

<?php
// CONNECT TO DATABASE...
// INCLUDE UPLOAD CLASS LIBRARY
include (dirname(__FILE__).'/lib/class.upload.php');


$files = array();
foreach ($_FILES['fileupload'] as $k => $l)
{
    foreach ($l as $i => $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->uploaded) {
            $this_upload = array();

            ///// 1 ////////////////////////////////////////////////////////////////////
            $handle->file_new_name_body   = $filenamex_thumb;
            $handle->file_force_extension = true;
            $handle->image_resize         = true;
            $handle->image_x              = '300';
            $handle->image_ratio_y        = true;
            $handle->jpeg_quality = '100';

            // ABSOLUTE PATH BELOW
            $handle->process($absoRoot.'covers/thumbs/');
            ////////////////////////////////////////////////////////////////////////////
            if ($handle->processed) {

      // store the image filename
    $this_upload['image'] = $handle->file_dst_name; // Destination file name
    $this_upload['body'] = $handle->file_dst_name_body; // Destination file name body
    $this_upload['extension'] = $handle->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->clean();
        header("Location: ./upload.php");
                $message = "";
    } else {

         echo '  file not uploaded to the wanted location';
         echo '  Error: ' . $handle->error . '';

      }
} ?>

(I use the Upload Class by Colin Verot to handle image uploads, and their FAQ tutorial to handle MULTIPLE image uploads on this page, under: What about multiple uploads?)

This would work perfect if I were just uploading images, however I added the functionality of adding each image data to my database. & This is where it gets confusing.

I’m sure the key is placing the SQL query inside the right foreach, or perhaps making another one, but I’ve tried that & it only gives me 1 good result for either the image upload or the title, never for both.

I need to upload the image to the site, then store its data (including image path) to my database.

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:21:43+00:00Added an answer on June 6, 2026 at 10:21 am

    When you’re gathering the file information you’re overwriting $imagename on every loop so it will be assigned to the last one. Try attaching it to the $files variable (hopefully this doesn’t mess with the upload class you’re using).

    foreach ($l as $i => $v)
    {
        if (!array_key_exists($i, $files))
        $files[$i] = array();
        $files[$i][$k] = $v;
        $files[$i]['imagename'] = $_POST['keyword'][$i];
    }
    

    Then update your $sql string to reference that

    $sql = 'INSERT INTO cover (id, img, keyword, category_name, cat_id) 
         VALUES ("", "'.$this_upload['image'].'", "'.$file['imagename'].'", 
             "'.$category_name.'", "'.$category.'")';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Language: PHP / Using Class Upload by Colin Verot About: Multiple Uploading The code
Language: PHP I have a form which asks users for their educational details, course
I use the PHP language for file upload and want to know that the
I have a function in PHP language to create an xml file when requested.
Development language and DB: PHP/MySQL I have a table geo_places with something like 8
Language: OO PHP 5+ DB: MySQL I am trying to insert data from a
I came from PHP language(codeigniter), but now I learning ASP.Net MVC :) In PHP
I build database-driven web sites. Previously I have used Perl or PHP with MySQL.
My environment is Apache, DB is mySQL and language is PHP. What I need
This is language agnostic (but I am using PHP and MySQL if it helps).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.