Search code examples
phpimage-uploadingmultiple-file-upload

How to insert data into database in a multiple file input?


I have a code that lets a person upload multiple images through a form.

Problem is, the images upload fine on to the server but not sure how to get the images to be sent into the database.

PHP:

        else{ // No error found! Move uploaded files 
            if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $targetscreenshots.$name)) 

            $count++; // Number of successfully uploaded file

        }

Where do I put the following code?

        {
mysql_query("INSERT into Colleges (`files`) VALUES ('$files')"); // inserting data if file is moved 
    echo "Your screenshots have been uploaded successfully!"
        }

Solution

  • This is my own code which i am using in my script.

            <?php
                 $upath="../images/";
                //uploads is the name of file array that is being uploaded.
            foreach ($_FILES['uploads']['name'] as $key=>$file) {
                $target = $upath.$file;
                $path=substr($target,3);
                // echo $path; THIS CAN BE STORED DIRECTLY TO THE DATABASE
                move_uploaded_file($_FILES['uploads']['tmp_name'][$key], $target)
                or die();
                mysql_query(**YOUR INSERT QUERY HERE. IT WONT BE EXECUTED IF IMAGE IS NOT UPLOADED PROPERLY.**)or die(mysql_error());
    
            }
    
            ?>
    

    I read your comment and so i gave this answer... Kindly correct me if i have misinterpreted your question.