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!"
        }
thomas
  • 163
  • 2
  • 13
  • 1
    Why not save image address in the database and save images on the server? – K.I Jun 08 '16 at 15:33
  • 1
    Suggest you look [here](http://stackoverflow.com/questions/5863870/how-should-a-model-be-structured-in-mvc) – bated Jun 08 '16 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. – thomas Jun 08 '16 at 15:47
  • @bated - Not sure exactly what that post is discussing and how it relates to my problem.... – thomas Jun 08 '16 at 15:47

3 Answers3

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.

Tirthraj Barot
  • 2,671
  • 2
  • 17
  • 33
  • 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. – thomas Jun 09 '16 at 15:49
  • 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 – Tirthraj Barot Jun 09 '16 at 15:54
  • 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? – thomas Jun 09 '16 at 16:11
  • Yes. Exactly Before increment. – Tirthraj Barot Jun 09 '16 at 16:13
  • ok, now 2 problems: 1) I get "Array" where the image name should be. 2) I get multiple rows created instead of just one. – thomas Jun 09 '16 at 16:39
  • So you want paths of all images to be inside a single record? – Tirthraj Barot Jun 09 '16 at 16:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114260/discussion-between-tirthraj-barot-and-thomas). – Tirthraj Barot Jun 09 '16 at 16:42
  • worked great except each image was sent to the database in each row and we agreed it would not be a good idea to have the image names separated by commas.. and the code, move_uploaded_file($_FILES['uploads']['tmp_name'][$key], $target), should be move_uploaded_file($_FILES['uploads']['tmp_name'][$key], $target.$file). working on setting up a new database table specifically for the images. – thomas Jun 09 '16 at 18:14
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.

K.I
  • 568
  • 1
  • 6
  • 19
  • 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. – thomas Jun 09 '16 at 15:48
0

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

Just Insert the file path or name in the DB

  • 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. – thomas Jun 09 '16 at 15:44