Jump to content

Multiple Images in Database


Adamm99

Recommended Posts

Hello,

I am new to PHP coding, and I have some issues trying to upload multiple images into database. The code works only for 1 image, but when I try to add foreach, I receive this message: Warning: Invalid argument supplied for foreach() .

Can anyone help me ?

problem.jpg

Link to comment
Share on other sites

First please do not post images of your code. Post the code itself using the code icon (<>) in the menu and specify PHP.

You're confused about $_FILES structure. Add the following code before the foreach so you can learn what $_FILES contain and adjust your loop accordingly.

echo "<pre>";
print_r($_FILES);
echo "</pre>";

 

Link to comment
Share on other sites

Sorry about the photo, I did not know.

Well, I am not confused about $_FILES, because like I said, the code works to upload only 1 image. I know what that code does, I do not understand why the loop does not work with it, and that is why I need help.

Link to comment
Share on other sites

Thank you Barand for your help.

I have write the code again and works, it seems that I forgot to put name='filestoupload[]'.  But there is one last problem, the code does not insert for example 1.jpg , 2 .jpg , 3.jpg in the same row, but it creates 3 different rows and I don't want that. Here is my new code : 
 

<?php 


 include "db.php";

 
if(!empty($_FILES['filestoUpload']['name'])){
    
  foreach($_FILES['filestoUpload']['name'] as $key => $val){
    
    $name = $_FILES['filestoUpload']['name'][$key];
    $tmp_loc = $_FILES['filestoUpload']['tmp_name'][$key];
    $size = $_FILES['filestoUpload']['size'];
    $type = $_FILES['filestoUpload']['type'];
    $fileExt = explode('.', $name);
    $fileActExt = strtolower(end($fileExt));
    $allowed = ['jpg', 'jpeg', 'png'];
    if(in_array($fileActExt, $allowed)){
        $query = "INSERT INTO list(images) VALUES ('$name')";
        $result = mysqli_query($conn, $query);
      } else {
          echo "This is not an image";
      }
    }
    
}  

 

Link to comment
Share on other sites

47 minutes ago, Adamm99 said:

but it creates 3 different rows and I don't want that.

Yes, you do. You are using a database, not a spreadsheet. Don't repeat multiple like columns in a single row. You should put them in separate rows along with the id of the parent record as a foreign key. Each row in a database table table should contain data about a single entity (in this case, an image). Read up on "Data normalization"

+----------------+
| tableA         |
+----------------+
| id      (PK)   |-----+
| etc.           |     |
+----------------+     |    +----------------+
                       |    |  list          |
                       |    +----------------+
                       +---<| tableA_id  (FK)|
                            | image          |
                            +----------------+

You can't rely on the file extension to be an accurate indication of the file type (You could rename a text file to "mytext.png" but that wouldn't make it an image.) The mime type is in the $_FILES array.

Edited by Barand
Link to comment
Share on other sites

I'm not the best resource on the block, but hopefully my experience can lead you in the right direction.

From what I see, you're problem is in this line (which, btw, is doing exactly what you've designed it to do):

Quote

$query = "INSERT INTO list(images) VALUES ('$name')";

You need to understand different concepts here.

First, what the UPLOAD code is doing is to grab a bunch of images (after overcoming your first problem in the post), get their names, check their extension and eventually insert into the db.

The catch is that it's grabbing the BUNCH and then going through this process FOR EACH; singularly.

One by one, the BUNCH is being handled and reviewed and ultimately inserted into the db until the same process repeats for the next image in the bunch.

Unless the image is rejected (ie: not a valid extension) it is written into the db on a new row. Then the loop goes back to the top for the next image.

 

Second, your QUERY is inserting into a column called NAME.  (Do you want the images to sit on each other's laps??? Of course not) if you truly want 3 images in one row (I wouldn't recommend it) then, at the very least, you would need to add fields that they could placed into; presumably name2 and name3.

Then it would be up to you to modify the code to either UPDATE the row during loop2 and loop3 OR create a loop that manages 3 cycles before a single INSERT into the db.

My suggestion would be to expand on what you've got and add fields that will help you regroup the three images later on (ie: username) assuming that's the reason you want then all in one row.

Another question would be whether you can guarantee that nobody has more than 3 images to submit. But if you give each image is own row, and reference the row to an identifier (username) they can submit unlimitedly, and you will be able to reference then accordingly.

Edited by phppup
Clean up post
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.