0

I am not very well in PHP. I want to upload two images separately and stored those in two different field in table. My HTML code is as below :

<form method="post" enctype="multipart/form-data" name="News Slider">
                <input name="img" type="file" required id="sortpicture"/>
                <input name="img2" type="file" required id="sortpicture"/>

                <button type="submit" name="submit" value="Save and Submit" class="btn btn-app">Save and Submit</button>
              <button type="reset" name="reset" value="Reset the Form" class="btn btn-app">Reset the Form</button>

              </form>

My table structure is like ImageId | image_name | image_thumbnail

I wrote code as below, but it stores only img2 (second selected image) image in image_name and image_thumbnail field. But img (first selected image) is not storing any where.

if(isset($_REQUEST['submit']))
     {

        if($_FILES['img']['name']!='')
    {
        $tmp_name = $_FILES["img"]["tmp_name"];
        $namefile = $_FILES["img"]["name"];
        $ext = end(explode(".", $namefile));
        $image_name=time().".".$ext;

        $fileUpload = move_uploaded_file($_FILES['img']['tmp_name'],"uploadnewsslider/".$image_name);


    }



    if($_FILES['img2']['name']!='')
    {
        $tmp_name = $_FILES["img2"]["tmp_name"];
        $namefile2 = $_FILES["img2"]["name"];
        $ext = end(explode(".", $namefile2));
        $image_thumbnail=time().".".$ext;

        $fileUpload2 = move_uploaded_file($_FILES['img2']['tmp_name'],"uploadnewsslider/".$image_thumbnail);
    }


         $sql="insert INTO newsslider(image, thumbnail) VALUES ('$image_name', '$image_thumbnail')";
         mysql_query($sql,$connection);
         }

In detail, I want to upload two images from two separate browse button from my form and then store those into two separate fields (i.e. image_name and image_thumbnail) in table.

Here you can see that I have two input file type (img and img2) and I want to store those in two different fields (image_name and image_thumbnail) in a table.

Please suggest. Thanks in advance.

Samiran
  • 37
  • 1
  • 8
  • 3
    Possible duplicate of [How can I store and retrieve images from a MySQL database using PHP?](http://stackoverflow.com/questions/1636877/how-can-i-store-and-retrieve-images-from-a-mysql-database-using-php) – Timofey Nov 29 '15 at 14:48
  • Dear, I dont want to store and retrieve solution of an single image. But I want to upload multiple image and store them in separate field in a table. – Samiran Nov 29 '15 at 15:03
  • What's the problem about copying the answer *twice* so you get two images instead of one? If you're not capable to do this, you should learn basics of PHP first. – Timofey Nov 29 '15 at 15:07
  • I did this, copying answer twice and renamed variables like : `if($_FILES['img2']['name']!='') { $tmp_name = $_FILES["img2"]["tmp_name"]; $namefile2 = $_FILES["img2"]["name"]; $ext = end(explode(".", $namefile2)); $image_thumbnail=time().".".$ext; $fileUpload2 = move_uploaded_file($_FILES['img2']['tmp_name'],"uploadnewsslider/".$image_thumbnail); } $sql="insert INTO newsslider(image, thumbnail) VALUES ('$image_name', '$image_thumbnail')"; mysql_query($sql,$connection); }` But it stores same images twice. Pls help – Samiran Nov 29 '15 at 15:15
  • Please update your question with the properly formatted code you're having problems with. Look at [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – Timofey Nov 29 '15 at 15:20
  • I have edited my question and tried to explain my problem in details. Please help. thanks in advance. – Samiran Nov 29 '15 at 15:42
  • Try [Using another answer](https://stackoverflow.com/questions/37375013/multiple-upload-images-into-mysql-database/47366585#47366585) multiple image – antelove Nov 20 '17 at 02:14

1 Answers1

1

You uploaded first file:

$fileUpload = move_uploaded_file($_FILES['img']['tmp_name'],"uploadnewsslider/".$image_name);

But you didn't insert it to the database.

So if you want to store two images in separate fields you need to:

  1. Modify your table schema so it can store two images instead of one(i.e. id | image1 | image1_thumb | image2 | image2_thumb)
  2. Give different names to the variables that hold the images (i.e. $image1_name, $image2_name etc)
  3. Insert both images in one query.

I hope it was clear.

Timofey
  • 823
  • 1
  • 8
  • 26
  • Thanks for your reply. I wrote query like `$sql="insert INTO newsslider(image, thumbnail) VALUES ('$image_name', '$image_thumbnail')"; mysql_query($sql,$connection);` I have also mentioned two different variables (img and img2) and table schema like `ImageID | image_name | image_thumbnail` Please clarify where I did wrong. – Samiran Nov 29 '15 at 16:55