By registering with us, you'll be able to discuss, share and private message with other members of our community.
SignUp Now!Very Helpful thanks!How about using a meaningful title?
I have made it a rule myself to only help people who can create decent posts.Very Helpful thanks!
Well, one thing that you should not be doing is that. Having fields like that in a table is not good. What you should do is have two tables that are linked.
User - ID, User
Image - ID, ImageID, Image
What he means is this:So how will I do this then? SOrry I dont have a know knowledge on SQL
Have you figured this out?
What he means is this:
table called "users" or something similar. This table stores things like "user_id", "user_name", "user_email" etc.
You would then have a 2nd table called "images" or something, which has "image_id, user_id, image_file".
Then you can use a SELECT statement to find a user's images ... "SELECT image_id, image_file FROM images WHERE user_id = 4"
It is the best way because then a user can have 0 to Unlimited images attached to their account whereas your idea requires a separate column per image in 1 table.
Solution 1:
Create a table with all the stated fields in
| Id | User | Img1 | Img2 | Img3 | Img4 | Img5 | Img6 | Img7 | Img8 | Img9 | Img10 |
- Negative
each user will only ever be able to have 8 images, if you need more it means changing the schema of the database to add extra columns. This is also very wasteful in terms of storage. if most users will only have 2 or 3 images then there are all those extra columns that will always be empty but taking up space.
- Positive
if you know that all users will always have 8 images then this is very optimised and is also very simple to work with and use.
Solution 2:
Create 2 tables, one for users and one for images
User Table:
| userID | userName | etc...
Images Table:
| imageID | userID | imageURL | imageName | etc...
each image in the image table has a userID that links back to the user table. This means when a user uploads a new image you add it to the images table and then link it back to the user with their ID. When someone wants to delete an image you just remove it from that table. This allows unlimited images per user with no schema changes.
Soam I going to use foreach then?
foreach ($_FILES["file_upload"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["file_upload"]["tmp_name"][$key];
$name = $_FILES["file_upload"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
}
<form action="insert_product.php" method="POST" enctype="multipart/form-data">
<label>File1: </label><input type="file" name="image1" />
<label>File2: </label><input type="file" name="image2" />
<label>File3: </label><input type="file" name="imag3" />
<label>File4: </label><input type="file" name="image4" />
<label>File5: </label><input type="file" name="imag5" />
<label>File6: </label><input type="file" name="image6" />
<input type="submit" />
</form>
<input type="file" id="files" name="image_files" multiple>
if(isset($_FILES['image_files'])){
if(is_array($_FILES['image_files']['size'])){
// multiple files uploaded
/*
format:
$_FILES['image_files'] = array(
"size"=>array("file1size", "file2size"),
"name"=>array("file1name", "file2name")
);
*/
$numfiles = count($_FILES['size']);
for($loopfiles = 0; $loopfiles < $numfiles; $loopfiles++){
/*
$_FILES['image_files']['size'][$loopfiles]
$_FILES['image_files']['name'][$loopfiles]
etc etc
Check that size is not too big, that name is valid, that file extension is valid, and upload each file in the loop
*/
}
} else {
// single file uploaded
// format: $_FILES['image_files'] = array("name"=>"name", "size"=>"size", etc)
// $_FILES['image_files']['name']
// $_FILES['image_files']['size']
// etc etc
// check to make sure size is ok, valid file format/extension, etc and upload single file
}
}
So, you do not need to put multiple file inputs, although you can.
The main reason to do multiple file inputs in the form is if you are wanting users to name each file, or submit specific settings for each file. If you are only wanting them to upload the files and there are no individual settings or pieces of info for each individual file then you can just use one file input box for uploading. You can use this:
Code:<input type="file" id="files" name="image_files" multiple>
The multiple tag allows the user to upload more than 1 file in 1 input box for files.
Then with PHP you can do something along the lines of:
PHP:if(isset($_FILES['image_files'])){ if(is_array($_FILES['image_files']['size'])){ // multiple files uploaded /* format: $_FILES['image_files'] = array( "size"=>array("file1size", "file2size"), "name"=>array("file1name", "file2name") ); */ $numfiles = count($_FILES['size']); for($loopfiles = 0; $loopfiles < $numfiles; $loopfiles++){ /* $_FILES['image_files']['size'][$loopfiles] $_FILES['image_files']['name'][$loopfiles] etc etc Check that size is not too big, that name is valid, that file extension is valid, and upload each file in the loop */ } } else { // single file uploaded // format: $_FILES['image_files'] = array("name"=>"name", "size"=>"size", etc) // $_FILES['image_files']['name'] // $_FILES['image_files']['size'] // etc etc // check to make sure size is ok, valid file format/extension, etc and upload single file } }
Code Forum is a community platform where coding enthusiasts can connect with other developers, engage in discussions, ask for help, and share their knowledge with a supportive community. It's a perfect place to improve your coding skills and to find a community of like-minded individuals who share your passion for coding.
We use essential cookies to make this site work, and optional cookies to enhance your experience.