1

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!"
        }
4
  • 1
    Why not save image address in the database and save images on the server? Commented Jun 8, 2016 at 15:33
  • 1
    Suggest you look here Commented Jun 8, 2016 at 15:38
  • @K.I - The way my code is supposed to work (at least for my single-image upload input code) is that the name of the image is sent and saved in the database and the image file itself is saved on the server. problem with the code above is I don't know how to send the image address/name to the database. Commented Jun 8, 2016 at 15:47
  • @bated - Not sure exactly what that post is discussing and how it relates to my problem.... Commented Jun 8, 2016 at 15:47

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

That code focuses on SINGLE-image uploads. The problem I have is focused on MULTIPLE-image uploads which includes have a loop. I'm having trouble figuring out where to put the mysql_query code to insert the image name into the database.
Certainly.. But the place to write remains the same... In the if condition where move uploaded file function is called.. If it is executed successfully, the database entry must be done.. @thomas
My code also uses a count++ increment. everytime, a file gets uploaded, another file if available is uploaded. do i put the database entry before or after the increment?
Yes. Exactly Before increment.
ok, now 2 problems: 1) I get "Array" where the image name should be. 2) I get multiple rows created instead of just one.
|
0

You are missing code that is responsible for database modification, I suggest you read some tutorials like this one. I haven't tested it, but at least it looks like it involves all the steps required.

1 Comment

That tutorial focuses on inserting SINGLE image into a server. My problems deals with inserting MULTIPLE images into a server. I'm having problem with one line of code which is inserting the image's file location on the server into the database.
0

$files = $_FILES["files"]["tmp_name"][$f]

Just Insert the file path or name in the DB

1 Comment

where do I put that code at? and I also need the actual php to INSERT the data into the database but I don't know where to put the code at. Your code doesn't really solve the data insertion into the database part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.