Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Mulitple image in a row!

How do I upload a multiple image and insert it to database in a 1 row !

Like this

| Id | User | Img1 | Img2 | Img3 | Img4 | Img5 | Img6 | Img7 | Img8 | Img9 | Img10 |


does " foreach " work or " for "
 
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
 
So how will I do this then? SOrry I dont have a know knowledge on SQL
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.
 
Have you figured this out?


Not yet!

Just thinking
Of on the sql putting multiple value :)


$image1 = file_get_contents ($_FILES['image']['tmp_name']);
$image2= file_get_contents ($_FILES['image']['tmp_name']);
$image3= file_get_contents ($_FILES['image']['tmp_name']);

What is need is

Upload the image one by one! Not in a a single upload
 
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.


Ill figure this out! DOes this work even on one by one button upload! Not on single button submit multiple upload!?



<form action="insert_product.php" method="POST" enctype="multipart/form-data">
<label>File1: </label><input type="file" name="image" />
<label>File2: </label><input type="file" name="image" />
<label>File3: </label><input type="file" name="image" />
<label>File4: </label><input type="file" name="image" />
<label>File5: </label><input type="file" name="image" />
<label>File6: </label><input type="file" name="image" />
<input type="submit" />
</form>
 
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>
 
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>

I think I misunderstood the original question. I thought you were talking about database architecture as opposed to the actual code to upload them as you have not even mentioned what language you are using and you have also tagged it as a database question.

From the provided code it looks like your using PHP, and I presume you're using MySQL. You cannot physically store images in a database. You have to store them on either a file server or in a root file within your project. The database just stores a file path / URL of that image along with a few other properties. For example, usually, you would automatically compress the image and store the original and the compressed versions in the same database row so that you can display the compressed version to the user. This would be the second solution from my earlier comment.

I recommend this upload class: https://www.verot.net/php_class_upload.htm?lang=en-GB. This is essentially an object you can call to upload any type of file you like in PHP. Read the documentation and you will very easily be able to start uploading files.

however, if you are not too sure on what loops are or how to use them then I think you may be getting over your head a bit. Uploading files is quite a higher level topic, I would recommend getting a bit more used to generic form data before moving on to uploads.
 
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
    }
}
 
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
    }
}


Thanks mate! I found out that putting imagein row is not good! Database will be laggy!
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom