PHP mysql and Multiple files upload

HI Guys.
i am trying to upload multiple files/images to my server and mysqli. it is working fine but the following is a problem. My unique ID`s starting from 1 as it is a new database. when i add product detail etc and then add lets say 5 pics it writes 5 separate lines into my database. i would like to have it as one database item.

<form action="" method="post" enctype="multipart/form-data">
    Select Image Files to Upload:
    <input type="file" name="files[]" multiple >
    <input type="submit" name="submit" value="UPLOAD">
    // Include the database configuration file 
    include_once 'dbconnection.php'; 
    // File upload configuration 
    $targetDir = "uploads/"; 
    $allowTypes = array('jpg','png','jpeg','gif'); 
    $statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = ''; 
    $fileNames = array_filter($_FILES['files']['name']); 
        foreach($_FILES['files']['name'] as $key=>$val){ 
            // File upload path 
            $fileName = basename($_FILES['files']['name'][$key]); 
            $targetFilePath = $targetDir . $fileName; 
            // Check whether file type is valid 
            $fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION); 
            if(in_array($fileType, $allowTypes)){ 
                // Upload file to server 
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetFilePath)){ 
                    // Image db insert sql 
                    $insertValuesSQL .= "('".$fileName."', NOW()),"; 
                    $errorUpload .= $_FILES['files']['name'][$key].' | '; 
                $errorUploadType .= $_FILES['files']['name'][$key].' | '; 
            $insertValuesSQL = trim($insertValuesSQL, ','); 
            // Insert image file name into database 
            $insert = $link6->query("INSERT INTO addequipment (file_name, uploaded_on) VALUES $insertValuesSQL"); 
                $errorUpload = !empty($errorUpload)?'Upload Error: '.trim($errorUpload, ' | '):''; 
                $errorUploadType = !empty($errorUploadType)?'File Type Error: '.trim($errorUploadType, ' | '):''; 
                $errorMsg = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType; 
                $statusMsg = "Files are uploaded successfully.".$errorMsg; 
                $statusMsg = "Sorry, there was an error uploading your file."; 
        $statusMsg = 'Please select a file to upload.'; 
    // Display status message 
    echo $statusMsg; 

An easy way to solve a problem like this is to print out your SQL to inspect what it looks like, and figure it out from there.

It appears that your SQL would look something like this:

INSERT INTO addequipment (file_name, uploaded_on) VALUES
('f1', NOW()), ('f2', NOW()), ('f3', NOW())...

And that’s how you do multiple inserts (link to Stackoverflow response). So, your code is doing multiple inserts because you’ve constructed your SQL to do that.

To fix your code, we need more info. What’s the schema for your table, and what values do you want inserted in your single row?

You really don’t - what you need is to have the details written once, and have the five images in a separate table, linked to the unique id generated in your “main” table. To store it in a single table row with a varying number of images, you either need:

  • A fixed number of columns, one for each image, meaning you have a limit on how many images per row you can have, and a relatively complicated means of adding or removing images (if you have five, and remove the third, do you shuffle four and five up to close the gap?).
  • Or you merge the image names into a single comma-separated column in your table, which is a really bad idea in terms of searching, adding or removing an image, all sorts.

Have a unique id in your addequipment table, and use this in your equipmentimages table, which will look like:

id        unique image id
equipid   id from `addequipment` table to link images to their parent item
name      image name / url

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.