php - How to check if no image is selected don't update? -


i working on project update function using pdo update function working fine, problem when try update record , stay , when see database image column emplty..how can check if file empty , when empty not update?because when try update record image , hit update button image gone..

user.class.php

public function upload($id,$file_name,$file_size,$file_type,$username,$password,$province) { if(empty($_file['myimage']['name'])){ echo "error"; } else { $stmt = $this->db->prepare("update tish_images set  file_name=:file_name,file_size=:file_size,file_type=:file_type,username=:username,password=:password,province=:province id=:id");  $errors= array(); foreach($_files['files']['tmp_name'] $key => $error ) {     if ($error != upload_err_ok)     {         $errors[] = $_files['files']['name'][$key] . ' not uploaded.';         //continue;         return false;     }     $file_name = $key.$_files['files']['name'][$key];     $file_size = $_files['files']['size'][$key];     $file_tmp  = $_files['files']['tmp_name'][$key];     $file_type = $_files['files']['type'][$key];       if($file_size > 2097152)     {         $errors[] = 'file size must less 2 mb';         //continue;         return false;     }      try     {         $stmt->bindparam( ":id", $id);         $stmt->bindparam( ":file_name", $file_name, pdo::param_str );         $stmt->bindparam( ":file_size", $file_size, pdo::param_str );         $stmt->bindparam( ":file_type", $file_type, pdo::param_str );         $stmt->bindparam( ":username", $username);         $stmt->bindparam( ":password", $password);         $stmt->bindparam( ":province", $province);         $stmt->execute();          $desired_dir="image_uploads";          if(is_dir($desired_dir)==false)         {             mkdir($desired_dir, 0700);// create directory if not exist         }         if(is_file($desired_dir.'/'.$file_name)==false)         {             move_uploaded_file($file_tmp,$desired_dir.'/'.$file_name);             return true;         }         else         {    //rename file if 1 exist             $new_file=$desired_dir.'/'.$file_name.time();             move_uploaded_file($file_tmp,$new_file);               return true;                      }     }     catch(pdoexception $e)     {       echo $e->getmessage();       return false;     }    } } 

}

update.php

<?php include_once 'db.php';  $username = isset($_get['username']) ? $_get['username'] : ''; $password = isset($_get['password']) ? $_get['password'] : ''; $province = isset($_get['province']) ? $_get['province'] : ''; $file_name = isset($_get['file_name']) ? $_get['file_name'] : ''; $file_size = isset($_get['file_size']) ? $_get['file_size'] : ''; $file_type = isset($_get['file_type']) ? $_get['file_type'] : '';    if(isset($_files['files'])){ $id = $_get['id']; $username = $_post['username']; $password = $_post['password']; $province = $_post['province'];  if($crud->upload($id,$file_name,$file_size,$file_type,$username,$password,$province)) {     echo "<script type='text/javascript'>alert('successfully updated!');</script>"; } else {     echo "<script type='text/javascript'>alert('updating failed!');</script>"; }  } if(isset($_get['id']))     {     $id = $_get['id'];     extract($crud->getid($id));     } ?>            <?php echo $file_name; ?>          <br />          <br />          <input type="file" name="files[]"  value="" multiple/>          <br />          <br />          <input type="text" name="username" value="<?php echo $username; ?>">          <br />          <br />          <input type="text" name="password" value="<?php echo $password ?>">          <br />          <br />          <input type="text" name="province" value="<?php echo $province ?>">          <br />          <input type="submit"/> 

i'm assuming you're getting image <input type="file" /> tag , want check if image selected. can try use empty() , $_file check if image selected.

example:

<?php     if(empty($_file['myimage']['name'])):         //don't update , display error message.     else:         //update record.     endif; ?> 

* note: if want accept images can html <input type="file" name="imagefilename" accept="image/x-png, image/gif, image/jpeg" />. don't forget validate in php before insert new record or uploading file selected directory.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -