2

I am having trouble getting a picture to load from mysql database. The directory is randomly generated and gets stored in the database just fine. When the page refreshes the img returns a broken link, echos 'not set.', and inspect element tells me that $default_pic isn't defined. I can't figure out what is going on here can anyone help?

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$listingid = $_SESSION['edit_listing'];
if(isset($_FILES['listingpic'])){
    if($_FILES['listingpic']['type']=="image/jpeg"||$_FILES['listingpic']['type']=="image/png"||$_FILES['listingpic']['type']=="image/gif"){
        if($_FILES['listingpic']['size']<1048576){
            $chars = "abcdefghijklmanopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
            $rand_dir_name = substr (str_shuffle($chars), 0, 15);
                                    mkdir("userdata/listingpics/$rand_dir_name/") or die("directory error");
            if(file_exists("userdata/listingpics/$rand_dir_name/".$_FILES['listingpic']['name'])){
                echo "File already exists.";
            }
            else{
                move_uploaded_file($_FILES['listingpic']['tmp_name'], "userdata/listingpics/$rand_dir_name/".$_FILES['listingpic']['name']) or die("Failed to move file.");
                $listing_pic_name = $_FILES['listingpic']['name'];
                $listing_pic_query = mysql_query("UPDATE properties SET default_pic='$rand_dir_name/$listing_pic_name' WHERE id='$listingid'"); 
                $check_def = mysql_query("SELECT default_pic FROM properties WHERE id='$listing_id'"); //ADDED
                $def_rows = mysql_fetch_assoc($check_def); //ADDED
                $def_pic = $def_rows['default_pic']; //ADDED
                $default_pic = "userdata/listingpics/".$def_pic;//<-PROBLEM
                header("Location: ../list_property/upload.php?id=".$listingid);
            }
        } else echo "File must not exceed 1MB.";
    } else echo "File must be a JPEG, PNG, or GIF image.";
} else echo "Not set.";

?>
<form action="" method="POST" enctype="multipart/form-data">
    <img src="<?php echo $default_pic; ?>" width="50%" height="50%"/><br>
    <br>
    <input type="file" name="listingpic" />
    <input type="submit" name="uploadpic" value="Upload Picture">
</form>
1
  • At the start it will Not be set as you are Filling form for 1st time. Commented Jun 28, 2015 at 8:14

1 Answer 1

2

When the you refresh the page after image has uploaded, the script refreshes, which means your $default_pic gets reset, so it gets empty.

If you want not to get rid of that error, you should write a query that pulls from database a default image and fills in variable.

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

3 Comments

I added... $check_def = mysql_query("SELECT default_pic FROM properties WHERE id='$listing_id'"); $def_rows = mysql_fetch_assoc($check_def); $def_pic = $def_rows['default_pic']; $default_pic = "userdata/listingpics/".$def_pic; still getting undefined error
to where you added it? also, update your code in your original post
You need to add that code to the end of your first if statement (means to where echo "not set") is done, because when no file is uploaded, the first if statement is not passing, thats where you say else { fetch default pic from mysql }

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.