【问题标题】:How to update table with new image data using codeigniter如何使用 codeigniter 用新的图像数据更新表
【发布时间】:2017-10-08 08:10:06
【问题描述】:

我有一个表格来上传图像路径和其他图像数据。当我选择上传新图像时,这很有效。

如果我提交表单而不选择新图像,如何防止我的图像路径设置为空白?

这是我的控制器:

    function updatewaterpurifier($id)
    {
        $name=$this->input->post('name');

        $this->load->model('user');  
        //$name =  $this->input->post('name');



        if($this->input->post('submit'))
        {

        //Check whether user upload picture
            if(!empty($_FILES['picture']['name'])){
                $new_file_name  = $this->input->post('name');
            $config['upload_path'] = 'uploads/images/';
            $config['allowed_types'] = 'jpg|jpeg|png|gif|pdf';

            $config['file_name'] = $new_file_name;
            $config['max_size']      = 2048;
            //Load upload library and initialize configuration
            $this->load->library('upload',$config);
            $this->upload->initialize($config);

            if($this->upload->do_upload('picture')){
                $uploadData = $this->upload->data();
                $picture = $uploadData['file_name'];
            }else{
                $picture = '';
            }
        }else{
            $picture = '';
        }
            //Prepare array of user data
            $userData = array(
                'product_brand'=> $this->input->post('brand'),
                'product_name' => $this->input->post('name'),
                'product_price' => $this->input->post('price'),
                'product_techno' => $this->input->post('technology'),
                'product_des' => $this->input->post('description'),
                'product_image' => $picture
            );
                $this->db->where('id', $id);
                $this->db->update('products', $userData);
            //Pass user data to model


            //Storing insertion status message.

        }

    //Form for adding user data
        $this->load->model('user'); 
        $this->data['h']= $this->user->editpurifier($id);
        $this->load->view('editwaterpurifier', $this->data, FALSE);



    }

【问题讨论】:

  • 只是为了指出你正在加载$this->load->model('user'); 两次,为什么不将它放在控制器的 __construct 区域中,这样只需加载一次。 codeigniter.com/user_guide/general/…
  • @Adi Singh:在你的代码中,如果你没有上传图像,你将存储 $picture 变量为空并将其传递给你的更新数据。因此,删除该部分即可。
  • else part ko remove kar du @Sucharitha
  • @AdiSingh:是的,该部分不是必需的,然后在您的 $userData 数组中根据条件添加 $picture 数据。
  • 我通过什么我想通过我的旧图像路径

标签: php codeigniter file-upload


【解决方案1】:

如果您在没有之前上传图片的情况下更新表格,则您的变量 $picture=''。使用它从数组$userData() 中取消设置product_image:

$userData = array(
  'product_brand'=> $this->input->post('brand'),
  'product_name' => $this->input->post('name'),
  'product_price' => $this->input->post('price'),
  'product_techno' => $this->input->post('technology'),
  'product_des' => $this->input->post('description'),
  'product_image' => $picture
);
if ($picture==''){
  unset($userData['product_image']);
}

这会从数组中删除product_image,并且不会将图像路径更新为空白。

【讨论】:

    【解决方案2】:

    在控制器中

     function updatewaterpurifier($id)
        {
    
            $this->load->model('user');  
    
            if($this->input->post('submit'))
            {
            //Prepare array of user data
                $userData = array(
                    'product_brand'=> $this->input->post('brand'),
                    'product_name' => $this->input->post('name'),
                    'product_price' => $this->input->post('price'),
                    'product_techno' => $this->input->post('technology'),
                    'product_des' => $this->input->post('description')
                );
             $name=$this->input->post('name');
    
            //Check whether user upload picture
             if(!empty($_FILES['picture']['name']))
              {
                $new_file_name  = $this->input->post('name');
                $config['upload_path'] = 'uploads/images/';
                $config['allowed_types'] = 'jpg|jpeg|png|gif|pdf';
    
                $config['file_name'] = $new_file_name;
                $config['max_size']      = 2048;
                //Load upload library and initialize configuration
                $this->load->library('upload',$config);
                $this->upload->initialize($config);
                if($this->upload->do_upload('picture'))
                {
                    $uploadData = $this->upload->data();
                    $picture = $uploadData['file_name'];
                    $userData['product_image']=$picture;
                }
               }
    
            //Pass user data to model
             $this->user->update_purifier($id,$userData);
    
            }
    
        //Form for adding user data
            $this->data['h']= $this->user->editpurifier($id);
            $this->load->view('editwaterpurifier', $this->data, FALSE);
    
    
    
        }
    

    型号

    public function update_purifier($id,$userData)
        {
    
                    $this->db->where('id', $id);
                    $this->db->update('products', $userData);
        }
    

    检查产品图片名称是否包含扩展名。如果不添加

    $ext1 = explode('.',$_FILES['picture']['name']);
    $ext2 = array_reverse($ext1);
    $extention = strtolower($ext2[0]);
    $config['file_name'] = $new_file_name.'.'.$extention;   
    

    【讨论】:

      猜你喜欢
      • 2019-03-03
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 1970-01-01
      相关资源
      最近更新 更多