Hi ,
In a form , I have a field for image , in edit page, i can display that image, when I am clicking update, without modifying image it shows error in image.
How to save previous image if its not changed.

Sample Code:

<span><img src="profile/<?=$rows["upload"]; ?>" id="blah" style="height:25%; width:50%;"><br />
        <input type="file"  name="upload" id="upload" onChange="validation();putImage();" style="margin-top:10%;" tabindex="12"/>
        </span>
 Update page:
 <?php
 include('connect.php');
 $random_no=rand(10000000,99999999);
$filename = $_FILES['upload']['tmp_name'];
//echo $filename;
//exit;
if(!empty($filename))
{   
$file_size = $_FILES['upload']['size'];
if($file_size> 1048576)
{
echo "<script type='text/javascript'>alert('File Size Exceed 1 Mb!'); window.location.href='preview.php';</script>";
}
else
{
$target_path = "profile/";
$uploaded_files=$random_no."_".$_FILES['upload']['name'];
move_uploaded_file($_FILES['upload']['tmp_name'], $target_path .$random_no."_".$_FILES['upload']['name']);
}
}

$newupid=mysql_query("update candidate setupload='$uploaded_files' where id='$upid'")or die(mysql_error());
echo "<script type='text/javascript'> alert('Updated Successfully'); window.location.href='otpreview.php';</script>";
?>

Based on your code, when user did not update the profile picture, the input type="file" name="upload" will be empty field where after being posted to the php, the checking on if(!empty($filename)) will be false and do nothing but still, it will proceed to $newupid=mysql_query("update candidate setupload='$uploaded_files' where id='$upid'") where the $uploaded_files is probably null and causing error in the end.

you can try using

is_uploaded_file('some_file');

to validate if the file was uploaded.

Another alternative way of doing this is to check if the file has been moved or not.

if(move_uploaded_file()){
    //do whatever is needed to done

}

you can also check based on the user's input,

$there_is_file = (!empty($_POST['upload']) ? true : false );

if($there_is_file){

    //process file
}

else{

    //leave it alone on update

}    

you can use this code:
first line of existing image is that image, which always fetch from database.

$existing_image = $ri['upload'];

if(isset($_FILES['upload']) && ($_FILES['upload']['size']>0)){
        if(trim($existing_image)!='1'){$photo=$existing_image;}
    else { $photo = date('dmY-his');
    $photo .="-".$_FILES['upload']['name'];

    }
    move_uploaded_file($_FILES['upload']['tmp_name'], 'profile/'.$photo);
    }
    else { $photo = $existing_image; }
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.