如何使用codeigniter更新新图像数据表
问题描述:
我有一个表单上传图像路径和其他图像数据。当我选择上传新图片时,这很有效。如何使用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);
}
答
在控制器
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
答
如果你没有以前的图片上传更新您的表格,您的变量$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图像路径为空白。
只需指出您正在加载'$ 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 –