//i have a problem i have made an edit page and it is working properly and i have done image uploading in it but if the user updates without uploading any image its previous image is removed can any body help me how to get the previous image my code is here.

if(isset($_REQUEST['update']))
{

$sid=$_REQUEST['id'];
$sql="SELECT * FROM tbl_student WHERE std_id='$sid'"; 
$result=mysql_query($sql);


// $stdid=$_REQUEST['studentid'];
$stdfname=$_REQUEST['fname'];
$stdlname=$_REQUEST['lname'];
$stdreg=$_REQUEST['reg'];
$stddob=$_REQUEST['dob'];
$stddoj=$_REQUEST['doj']; 
$name=$_FILES['uploadedfile']['name'];
$type=$_FILES['uploadedfile']['type'];
$size=$_FILES['uploadedfile']['size'];
$check=$_FILES['uploadedfile']['name'];
$uname=str_replace(" ","_",$name);
$tmp_name=$_FILES['uploadedfile']['tmp_name'];
$target_path = "uploads/";
//$file=getimagesize($target_path);
$target_path = $target_path . basename($uname);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) 
{
list($width,$height,$siz,$attr)=getimagesize($target_path);
if($siz==0)
{
echo "You have not chosen an image please select an image or select your Previous image";
?>
Please<a href="">Click Here</a> to return to page

<?php }
/*if(getimagesize($target_path)=="")
{
$sq="SELECT name,type,size FROM tbl_student WHERE std_id='$sid'";
$res=mysql_query($sq);
$arr=mysql_fetch_array($res);
echo $filename=$arr['name'];
echo $filetype=$arr['type'];
echo $filesize=$arr['size'];
$sql = "UPDATE tbl_student SET first_name='".$stdfname."',last_name='".$stdlname."',reg_number='".$stdreg."',date_of_birth='".$stddob."',date_of_joining='".$stddoj."',name='".$filename."',type='".$filetype."',size='".$filesize."' WHERE std_id='$sid'"; 
$result=mysql_query($sql);
}*/
if($width>150||$height>200)
 {
 echo "Your image size has exceeded the maximum length please re-enter";?>
 Please <a href="?id=<?php echo $_REQUEST['id']; ?>">Click here</a>to return to the page
 <?php die(); ?>
 
  
<?php }
if($siz>350000)
{
echo "Size is too big"; ?>
 Please <a href="?id=<?php echo $_REQUEST['id'];?> ">Click here</a>to return to the page
<?php die();
}
if(!( $type=='image/jpeg' || $type=='image/png' || $type=='image/gif'||$type=='image/jpg' ))
{
echo "Your input format is not valid";
?>
 Please <a href="?id=<?php echo $_REQUEST['id']; ?>">Click here</a>to return to the page
<?php die();
}
}
$sql = "UPDATE tbl_student SET first_name='".$stdfname."',last_name='".$stdlname."',reg_number='".$stdreg."',date_of_birth='".$stddob."',date_of_joining='".$stddoj."',name='".$uname."',type='".$type."',size='".$size."' WHERE std_id='$sid'"; 
$result=mysql_query($sql);
  
}

?>

Hi,

Try this

$target_path = $target_path . basename($uname);

$newname = dirname(__FILE__).'/images/'.$filename;
//Check if the file with the same name is already exists on the server

if (!file_exists($newname)) {
   //Attempt to move the uploaded file to it's new place
   if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
    // your upload code here...
   }
}

you need to do a check i.e if file is empty and then it will update the database without effecting image field in the database and also if file is empty then no need of move_uploaded_file function.

thanks a lot i got my problem solved

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.