简体   繁体   中英

How to update database with old image and without choosing new image?

I am trying to edit employees. I am deleting the old file if a new file is uploaded with new name form. This works well except updating with old image. I watched some tutorials and examples from questions on Stackoverflow but they couldn't solve my problem.

My question is:

How to update rows with existing image if user wants to keep their old image?

Here is my update page:

    // This part select old image
 try{
  $id = $_REQUEST['update_id']; 
  $select_stmt = $pdo->prepare('SELECT * FROM employees WHERE id =:id'); 
  $select_stmt->bindParam(':id',$id);
  $select_stmt->execute(); 
  $row = $select_stmt->fetch(PDO::FETCH_ASSOC);
  extract($row);
 }catch(PDOException $e){
  $e->getMessage();
 }

}

if(isset($_REQUEST['btn_update'])){
 try{
  $name = $_REQUEST['name'];
  $address = $_REQUEST['address'];
  $salary = $_REQUEST['salary'];

    // This part giving new name to image and validation.
  $image_file = generatenewstring(12).$_FILES["image"]["name"];
  $type  = $_FILES["image"]["type"]; 
  $size  = $_FILES["image"]["size"];
  $temp  = $_FILES["image"]["tmp_name"];

  $path="../images/ilanlar/".$image_file; 

  $directory="../images/ilanlar/"; 

  if($image_file){
   if($type=="image/jpg" || $type=='image/jpeg' || $type=='image/png' || $type=='image/gif'){ 
    // Checking for image if exist we will delete this statment later on
    if(!file_exists($path)){
     if($size < 1000000){
    // deleting old image
      unlink($directory.$row['image']); 
    // uploading new image
      move_uploaded_file($temp, "../images/ilanlar/" .$image_file); 
     }else{
      $errorMsg = "Your File To large Please Upload 5MB Size";
     }
    }else{ 
     $errorMsg = "File Already Exists...Check Upload Folder";
    }
   }else{
    $errorMsg = "Upload JPG, JPEG, PNG & GIF File Formate.....CHECK FILE EXTENSION";
   }
  }else{
   $image_file = $row['image']; 
  }

  if(!isset($errorMsg)){
   $stmt=$pdo->prepare('UPDATE employees SET 
                    name=:name, address=:address, salary=:salary, image=:image 
                    WHERE id=:id'); 
   $stmt->bindParam(':name',$name);
   $stmt->bindParam(':address',$address); 
   $stmt->bindParam(':salary',$salary);
   $stmt->bindParam(':image',$image_file);
   $stmt->bindParam(':id',$id);
    //bind all parameters
   if($stmt->execute()){
    echo "File Update Successfully......."; 
    header("refresh:3;3.php"); 
   }
  }
 }catch(PDOException $e){
  echo $e->getMessage();
 }

}

Thanks for any help.

The code I tried was too long and had to do a new query each time:

if ($image == 0) {
 //query1
} else if ($image == 1) {
 //query2
} else {
 //query3
}

What's goiing wrong:

When you DON'T upload a new file, you still run

$image_file = generatenewstring(12).$_FILES["image"]["name"];
// snip
if ($image_file) {
    // Processing image & error handling
}

I assume that generatenewstring(12) generates a string with 12 random characters, so $image_file will ALWAYS yield to true, therefore always executing your if-statement.

Your type-check will fail, so $errorMsg will be set to "Upload JPG, [snip]" . Therefore

if(!isset($errorMsg)) { 
    // Update-statement
}

won't be executed.

Solution:

Change if ($image_file) to if(is_uploaded_file($_FILES["image"]["tmp_name"])) . In that case that whole block won't be executed when you don't upload anything, so $errorMsg won't be set.

You probably also wan't to:

1) Move some code (that whole block isn't necessary when you don't upload)

if(is_uploaded_file($_FILES["image"]["tmp_name"])) {
    $image_file = ...
    $type = ...
    // [snip]
    $directory = ...

    if ($type == "image/jpg") {
    }
} else {
    $image_file = $row['image'];
}

2) Echo your $errorMsg somewhere ;)

3) You could leave that COALESCE from the comments out, you already resolved that issue by setting $image_file to the current value in your else-statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM