I am trying to save 2 photos to my MySQL database. I can only see one of the 2 photos in the database (i.e. its name) but I see the 2 photos in the folder I store my photos. ( A picture of how the database looks like is attached.) When I try to display the 2 photos, I only see that one photo that I saw in the database. What is the problem here?

Html Code to upload photo

<input type="file" id="_photo"  name="_photo[]" multiple="multiple">
<input type="button" id="add_more" class="" value="Add More Files"/>

Code to save photos in database and folder

for ($i = 0; $i < count($_FILES["_photo"]["name"]); $i++) {  
    if (!empty($_FILES['_photo']['name'][$i])) {   
        if ($_FILES['_photo']['type'][$i] == 'image/jpeg') {
            $upload_folder = "./profile_pix/";
            $pic_name = time() . ".jpg";
            $pic_path = $upload_folder . $pic_name;
            require_once "include/resize.php";
                if (move_uploaded_file($_FILES['_photo']['tmp_name'][$i], $pic_path)) {
                    $image = new Resize($pic_path);
                    $image->resizeImage(180, 180, 'crop');
                    $image->saveImage($pic_path);
                    //thumbnail
                    $image = new Resize($pic_path);
                    $image->resizeImage(50, 50, 'crop');
                    $image->saveImage($upload_folder . "thumb/" . $pic_name);
                }
            }   
    } 
    else {          
            $pic_name="default_house.png";          
    }
} 

Code to display Uploaded Photos

$sql = "SELECT * FROM myDatabase";
foreach ($db->query($sql) AS $result){
      echo "
       <img src='profile_pix/{$result['photos']}' width = '30' height ='30px' style=' margin-top:8px;'/>
       ";
 }

Recommended Answers

All 2 Replies

The problem might be occuring at the time of saving pictures in the database (there are two photos in the folder but only one in the database). Post the code for saving the data into the database (I have a feeling that the insert query should be within the for loop).

As like broj1 mentioned the problem might be occuring at the time of saving pictures in the database. So the insert query should be within the for loop.
here is an example.

<?php
for ($i = 0; $i < count($_FILES["_photo"]["name"]); $i++) {  
    if (!empty($_FILES['_photo']['name'][$i])) {   
        if ($_FILES['_photo']['type'][$i] == 'image/jpeg') {
            $upload_folder = "./profile_pix/";
            $pic_name = time() . ".jpg";
            $pic_path = $upload_folder . $pic_name;
            require_once "include/resize.php";
                if (move_uploaded_file($_FILES['_photo']['tmp_name'][$i], $pic_path)) {
                    $image = new Resize($pic_path);
                    $image->resizeImage(180, 180, 'crop');
                    $image->saveImage($pic_path);
                    //thumbnail
                    $image = new Resize($pic_path);
                    $image->resizeImage(50, 50, 'crop');
                    $image->saveImage($upload_folder . "thumb/" . $pic_name);
                }
            }   
    } 
    else {          
            $pic_name="default_house.png";          
    }

    //insert query
    $insert=mysql_query("INSERT INTO table_name(field1) VALUES('$pic_name')");

} ?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.