2017-10-08 105 views
0

我有一個表單上傳圖像路徑和其他圖像數據。當我選擇上傳新圖片時,這很有效。如何使用codeigniter更新新圖像數據表

如果我提交表單而未選擇新圖像,如何防止將圖像路徑設置爲空白?

這裏是我的控制器:

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



    } 
+0

只需指出您正在加載'$ this-> load-> model('user');'兩次,爲什麼不將它放在控制器的__construct區域中,這樣只需加載一次。 http://www.codeigniter.com/user_guide/general/controllers.html#class-constructors – user4419336

+0

@Adi Singh:在你的代碼中,如果你沒有上傳圖片,你將存儲$ picture變量爲空,並在ur中傳遞更新數據。所以刪除它的工作部分。 – Sucharitha

+0

其他部分ko刪除kar du @Sucharitha –

回答

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; 
+0

其不工作的兄弟 –

+0

我編輯你的代碼..試試這個。 – Priyanka

0

如果你沒有以前的圖片上傳更新您的表格,您的變量$picture=''。用它來取消設置product_image從陣列$userData()

$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並不會updatinge圖像路徑爲空白。

相關問題