Adamm99 Posted March 16, 2021 Share Posted March 16, 2021 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/312301-multiple-images-in-database/ Share on other sites More sharing options...
gw1500se Posted March 16, 2021 Share Posted March 16, 2021 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/312301-multiple-images-in-database/#findComment-1585133 Share on other sites More sharing options...
Adamm99 Posted March 17, 2021 Author Share Posted March 17, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/312301-multiple-images-in-database/#findComment-1585150 Share on other sites More sharing options...
Barand Posted March 17, 2021 Share Posted March 17, 2021 Have you remembered the "[]" in the file input field name? <input type='file' name='filestoupload[]' multiple> Quote Link to comment https://forums.phpfreaks.com/topic/312301-multiple-images-in-database/#findComment-1585152 Share on other sites More sharing options...
Adamm99 Posted March 17, 2021 Author Share Posted March 17, 2021 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"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/312301-multiple-images-in-database/#findComment-1585163 Share on other sites More sharing options...
Barand Posted March 17, 2021 Share Posted March 17, 2021 (edited) 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 March 17, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/312301-multiple-images-in-database/#findComment-1585164 Share on other sites More sharing options...
phppup Posted March 17, 2021 Share Posted March 17, 2021 (edited) 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 March 17, 2021 by phppup Clean up post Quote Link to comment https://forums.phpfreaks.com/topic/312301-multiple-images-in-database/#findComment-1585165 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.