如何在codeigniter中更新图像?

1mrurvl1  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(354)

我创建了图像上传工作正常,但我也希望它被更新。当我添加需要的图像时,它会正确更新,但是如果我不想更改图像并保持原样,则无法检索我的当前图像。我只想有时更新我的工厂名称(字段名称)。当我更新映像时,它更新成功,但当我更新工厂名称时,只有现有映像丢失。请帮我怎么做?
这是我的视图文件

<form action="<?php echo base_url('login/insert_factory/'.$id); ?>" method="POST" enctype="multipart/form-data">
 <?php
           $id =$this->uri->segment(3);
           if($id == "")
            {
            ?>  
            <div class="form-group">
                <label class="form-label" for="field-1">Factory Name</label>
                <!--span class="desc">e.g. "Beautiful Mind"</span-->
                <div class="controls">
                <input type="text" name="factory_name" class="form-control" >
                </div>
            </div>
             <?php

                }
                else
               { ?> 
           <div class="form-group">
                <label class="form-label" for="field-1">Factory Name</label>
                <!--span class="desc">e.g. "Beautiful Mind"</span-->
                <div class="controls">
                    <input type="text" name="factory_name" value="<?php echo $result->factory_name; ?>" class="form-control" >
                </div>
            </div>
            <?php } ?>

           <?php
                            if($id == "")
                            {
                            ?>  

            <div class="form-group">
                <label class="form-label" for="field-1">Upload Logo</label>
                <!--span class="desc">e.g. "Beautiful Mind"</span-->
                <div class="controls">
                     <input type="file"  name="upload_logo" />
                </div>
            </div>
            <?php

                }
                else
               { ?> 
           <div class="form-group">
                <label class="form-label" for="field-1">Upload Logo</label>
                <!--span class="desc">e.g. "Beautiful Mind"</span-->
                <div class="controls">
                     <input type="file"  name="upload_logo" value="<?php echo $result->factory_logo; ?>" />
                </div>
            </div>
           <?php } ?>
           <?php
                            if($id == "")
                            {
                            ?>  
            <div class="form-group">
                <div class="controls">
            <input type="submit" class="btn btn-primary" value="submit" />        

                </div>
            </div>
            <?php

                }
                else
               { ?> 
           <div class="form-group">
                <div class="controls">
            <input type="submit" class="btn btn-primary" value="Update"  />        

                </div>
            </div>
             <?php } ?>
            </form>

这是我的控制器代码:

public function insert_factory()
     { 
        $id =$this->uri->segment(3);

        if($id == "")
        {
        if(isset($_FILES['upload_logo'])){

              $errors= array();
              $file_name = $_FILES['upload_logo']['name'];
              $file_tmp =$_FILES['upload_logo']['tmp_name'];

              if(empty($errors)==true){
                 move_uploaded_file($file_tmp,"assets/uploads/".$file_name);

                // echo "Success";
              }else{
                 print_r($errors);
              }

                $fields = array(
                   'factory_name' => $this->input->post('factory_name',True),
                   'factory_logo' => $file_name
                );

                $result = $this->login_model->insert_factory($fields);

                if($result){
                    $this->session->set_flashdata('success_msg',' Factory Created Successfully!');
                    redirect('login/view_factory');
                }else{
                    $this->session->set_flashdata('error_msg','Factory Cannot Created.!');
                    redirect('login/create_factory');
                }
            } 
        }else{

            if(isset($_FILES['upload_logo'])){

              $errors= array();
              $file_name = $_FILES['upload_logo']['name'];
              $file_tmp =$_FILES['upload_logo']['tmp_name'];

              if(empty($errors)==true){
                 move_uploaded_file($file_tmp,"assets/uploads/".$file_name);

                // echo "Success";
              }else{
                 print_r($errors);
              }

                $fields = array(
                   'factory_name' => $this->input->post('factory_name',True),
                   'factory_logo' => $file_name
                );

                $this->db->where('factory_id', $id);
                $this->db->update('create_factory', $fields);
                if($this->db->affected_rows()>0)
                {
                    $this->session->set_flashdata('success_msg',' Factory Updated Successfully!');
                }else{
                    $this->session->set_flashdata('error_msg','Factory Cannot Updated.!');
                    redirect('login/create_factory');
                }
                redirect(base_url('login/view_factory'));
        } 

        }
     }

这是我的模型文件:

public function insert_factory($fields)
        {
            $this->db->insert('create_factory',$fields);
            return ($this->db->affected_rows() != 1) ? false : true;
        }
4c8rllxm

4c8rllxm1#

在控制器的if和else块中,请修改现有代码,以便在未提供映像且仅提供工厂名称时不更新空映像。
if和else块中的代码:

$fields = array(
    'factory_name' => $this->input->post('factory_name',True),
    'factory_logo' => $file_name
);

需要更改为:

$fields['factory_name'] = $this->input->post('factory_name', True);

if (!empty($file_name)) {
    $fields['factory_logo'] = $file_name;
}

相关问题