0

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;'/>
       ";
 }
Attachments code.png 35.78 KB
3
Contributors
2
Replies
22
Views
2 Years
Discussion Span
Last Post by amith_ami
0

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).

0

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')");

} ?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.