我有一個表單上傳圖像路徑和其他圖像數據。當我選擇上傳新圖片時,這很有效。如何使用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);
}
只需指出您正在加載'$ this-> load-> model('user');'兩次,爲什麼不將它放在控制器的__construct區域中,這樣只需加載一次。 http://www.codeigniter.com/user_guide/general/controllers.html#class-constructors – user4419336
@Adi Singh:在你的代碼中,如果你沒有上傳圖片,你將存儲$ picture變量爲空,並在ur中傳遞更新數據。所以刪除它的工作部分。 – Sucharitha
其他部分ko刪除kar du @Sucharitha –