dongshao8471 2018-10-18 08:10
浏览 46

图像删除时更新php mysqli中的其他记录

While updating the record if I do not upload the image and click on update button the current image will be removed.
Here is the code

<?php

if (isset($_POST['update_record'])){

    $edit_id = $_GET['edit'];
    $username  = $_POST['name'];
    $email  = $_POST['email'];
    $city  = $_POST['city'];


    $file_name = $_FILES['file']['name'];
    $file_tmp_name = $_FILES['file']['tmp_name'];
    $file_error = $_FILES['file']['error'];
    $file_size = $_FILES['file']['size'];
    $file_type = $_FILES['file']['type'];

    $allowed = ['png' , 'jpg' , 'jpeg'];
    $pathinfo =   pathinfo($file_name , PATHINFO_EXTENSION);

    $destination = "uploads/" . $file_name ;

    if (in_array($pathinfo , $allowed)){
        if ($file_size < 100000){
            if ($file_error === 0){

                move_uploaded_file($file_tmp_name, $destination) ;
            } //error close here
            else{
                echo "Some kind of error";
            }
        } //size close here

        else{
            echo "File Size is too big!";
        }
    } //type close here

    else{
        echo "File type is wronng";
    }

    $update_query = "UPDATE crud 
                        SET `name`='$username' , `email`='$email' ,
                             `city`='$city', `image`='$destination' 
                     WHERE id=$edit_id";

    $run = mysqli_query($connect, $update_query) ;

    if ($run){
        header("Location: show_record.php") ;
    } else{
        echo "Error in Updating the data";
    }

} //main if isset close here
?>
  • 写回答

1条回答 默认 最新

  • donglin1192 2018-10-18 08:15
    关注

    verify that a file exists before proceeding. if no file exist, then DO NOT update the $destination in your update query

    //use a flag
    $fileExists = false;
    
    if(!empty($_FILES['file'])) { //check here
    $fileExists = true;
     //rest of the file upload code    
    }
    
    
    if($fileExists === true){
      // a file was uploaded. now update $destination variable in update query as well
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致