如何使用codeigniter使用新的图像数据更新表

时间:2017-10-08 08:10:07

标签: php codeigniter file-upload

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

如果我在没有选择新图片的情况下提交表单,如何防止我的图片路径设置为空白?

这是我的控制器:

    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);



    }

2 个答案:

答案 0 :(得分:0)

在控制器中

 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;   

答案 1 :(得分:0)

如果您在没有上传图片的情况下更新表格,那么您的变量$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,并且不会将图像路径更新为空白。