在PHP oops中使用图像更新数据时出现问题

在PHP oops中使用图像更新数据时出现问题,php,Php,我是PHP oops新手。我已经创建了一个表,其中包含id、category_name、category_image和date列的类别名称 当我试图编辑特定id的类别名称而不更改当前图像时,会出现错误。但当我同时更改类别名称和图像时,效果很好 这是我的分类课 <?php class Category extends DatabaseObject { static protected $table_name = 'category'; static pro

我是PHP oops新手。我已经创建了一个表,其中包含id、category_name、category_image和date列的类别名称

当我试图编辑特定id的类别名称而不更改当前图像时,会出现错误。但当我同时更改类别名称和图像时,效果很好

这是我的分类课

<?php

class Category extends DatabaseObject {

        static protected $table_name = 'category';
        static protected $db_columns = ['id','category_name','category_image','date'];


        public $id;
        public $category_name;
        public $category_image;
        public $date;
        public $tmp_path;
        public $upload_directory = "data/category";
        public $custom_errors = array();
        public $upload_errors_array = array(


          UPLOAD_ERR_OK           => "There is no error",
          UPLOAD_ERR_INI_SIZE       => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
          UPLOAD_ERR_FORM_SIZE    => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
          UPLOAD_ERR_PARTIAL      => "The uploaded file was only partially uploaded.",
          UPLOAD_ERR_NO_FILE      => "No file was uploaded.",               
          UPLOAD_ERR_NO_TMP_DIR   => "Missing a temporary folder.",
          UPLOAD_ERR_CANT_WRITE   => "Failed to write file to disk.",
          UPLOAD_ERR_EXTENSION    => "A PHP extension stopped the file upload."                 


        );


        public function __construct($args=[]){
            $this->category_name = $args['category_name'] ?? '';
            $this->category_image = $args['category_image'] ?? '';
            $this->date   = $args['date'] ?? '';
        }



        public function set_file($file) {

          if(empty($file) || !$file || !is_array($file)){

            $this->errors[] = "There was no file uploaded here";
            return false;

          }
          elseif($file['error'] !=0) {
            $this->errors[] = $this->upload_errors_array[$file['error']];
            return false;
          }
          else {
            $ext = pathinfo($file['name'] , PATHINFO_EXTENSION);
            $rand=rand(1111,9999);
          $this->category_image =  $this->category_name."-".rand().".".$ext;
          $this->tmp_path = $file['tmp_name'];

          }
        }



        public function picture_path() {

          return $this->upload_directory.DS.$this->category_image;
        }

        public  function save_photo() {

           if($this->id){
            $target_path = SITE_ROOT . DS . 'admin' .DS. $this->upload_directory .DS. $this->category_image;

            if(move_uploaded_file($this->tmp_path, $target_path)){
              if($this->update()){
                unset($this->tmp_path);
                return true;
              }
              else {
                $this->errors[] = "The file directory probably does not have permission";
                return false;
              }
            }

           }
           else{

             if(!empty($this->errors)){
               return false;
             }

             if(empty($this->category_image) || empty($this->tmp_path)){ 
               $this->errors[] = "The File was not available";
               return false;
             }

             if(is_dir($this->upload_directory)==false){
              mkdir($this->upload_directory, 0700);     // Create directory if it does not exist
            }
             $target_path = SITE_ROOT . DS . 'admin' .DS. $this->upload_directory .DS. $this->category_image;

             if(file_exists($target_path)) {
               $this->errors[] = "The file {$this->category_image} already exists";
               return false;
             }

             if(move_uploaded_file($this->tmp_path, $target_path)){
               if($this->create()){
                 unset($this->tmp_path);
                 return true;
               }
               else {
                 $this->errors[] = "The file directory probably does not have permission";
                 return false;
               }
             }
           }
        }
}

?>
这是更新的post请求

<?php
if(!isset($_GET['id'])) {
    redirect_to(url_for('category.php'));
  }
  $id = $_GET['id'];
   $category = Category::find_by_id($id);
      if($category == false){
          redirect_to(url_for('category.php'));
      }
if(is_post_request()) {

    // Create record using post parameters
    $args = $_POST['category'];
    $category->merge_attributes($args);
    if(is_uploaded_file($_FILES['cat_image'])){

  $category->set_file($_FILES['cat_image']);
  $result = $category->save_photo();


}
else{
  $result = $category->save();
}

    if($result === true) {
    //   $new_id = $project->id;
      $session->message('The Category was Updated successfully.');
      // echo  '<script>window.location.href = "./project.php";</script>' ;
      redirect_to(url_for('edit_category.php?id=' .$id));

    } else {
      // show errors 


    }

  } else {
    // display the form

  }
?>
没有上传任何文件时抛出错误


如果我没有选择任何文件或如果我选择了文件,我该怎么办

检查用户是否使用is_uploaded_file上传了文件。如果他们这样做了,就处理它。如果他们没有,什么也不做

 <?php
 ...
 if (is_post_request()) {

    // Do your normal `category` processing
    $args = $_POST['category'];
    $category->merge_attributes($args);

    // Only process the file if the user uploaded it. 
    if (is_uploaded_file($_FILES['cat_image']['tmp_name'])) {
        $category->set_file($_FILES['cat_image']);
        $result = $category->save_photo();
    }

    ...
}

如何检查图像是否已在数据库中

您需要执行SQL查询,以确定该列是否包含值

 <?php
 ...
 if (is_post_request()) {

    // Do your normal `category` processing
    $args = $_POST['category'];
    $category->merge_attributes($args);

    // Only process the file if the user uploaded it. 
    if (is_uploaded_file($_FILES['cat_image']['tmp_name'])) {

        // Check if image already exists. 
        // I do not know anything about your tables so I am just making one up here. 
        // Please don't ask me to write this for you. 
        // This is just pseudo code.
        $query = 'SELECT 1 FROM table WHERE id = ? AND image_url IS NOT NULL';

        if ($query->result == 1) {
          // The row already contains a value - do something. Or not...
        }

        $category->set_file($_FILES['cat_image']);
        $result = $category->save_photo();
    }

    ...
}

嗯,您总是调用方法set_file,如果没有发布任何文件,它将返回一个错误。您需要找出它是否是一个更新,或者创建一个有效的id,这是一个更新。如果你没有身份证,这是一个创造。如果是“创建”,则需要该文件。如果是更新,则完全忽略该文件(如果该文件为空)如果我未选择任何文件或如果我选择文件,该怎么办?如何检查图像是否已在数据库中,或者我可以做什么do@ShadowShoot-请使用更新的问题编辑您的问题。不要把它们贴在评论栏。@Waterlomatt我的问题和上面的问题一样