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.

7
  • 3
    Possible duplicate of How can I store and retrieve images from a MySQL database using PHP? Commented Nov 29, 2015 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. Commented Nov 29, 2015 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. Commented Nov 29, 2015 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 Commented Nov 29, 2015 at 15:15
  • Please update your question with the properly formatted code you're having problems with. Look at Minimal, Complete, and Verifiable example Commented Nov 29, 2015 at 15:20

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.