[英]Image column null if not update image codeigniter Php
I am trying to update firstname,lastname,image in codeigniter,But whenever i update only first_name and last_name(not image) then image column become null in mysql,Where i am wrong Here is my code 我正在尝试更新codeigniter中的名字,姓氏,图像,但是每当我只更新first_name和last_name(不是图像)时,image列在mysql中就为空,这是我错的地方这是我的代码
Here is my model 这是我的模特
$add_data['first_name'] = ($this->input->post('first_name') && !empty($this->input->post('first_name'))) ? $this->input->post('first_name') : NULL;
$add_data['last_name'] = ($this->input->post('last_name') && !empty($this->input->post('last_name'))) ? $this->input->post('last_name') : NULL;
$saveArr = [];
if(!empty($this->input->post('first_name'))){
$saveArr['first_name'] = $this->input->post('first_name');
}
if(!empty($this->input->post('last_name'))){
$saveArr['last_name'] = $this->input->post('last_name');
}
if(!file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name']))
{
$saveArr['image'] = $_FILES['image'];
}
else
{
$filename=time().uniqid(rand()).$_FILES['image']['name'];
move_uploaded_file($_FILES["image"]["tmp_name"], "images/" . $filename);
$saveArr['image']=$filename;
}
$this->db->where('id',$add_data['user_id']);
$query=$this->db->update('users', $saveArr);
From what I understand (I don't do PHP or CodeIgniter) your code always specifies the image column so it will always be updated. 据我了解(我不使用PHP或CodeIgniter),您的代码始终指定image列,因此将始终对其进行更新。 You need logic that will only update that column if you have a value.
您需要仅在拥有值的情况下才更新该列的逻辑。
In both condition you set $saveArr['image']
, you just need to set $saveArr['image']
when upload new image. 在这两个条件设置
$saveArr['image']
你只需要设置$saveArr['image']
上传新的图片时。 so just remove line $saveArr['image'] = $_FILES['image'];
所以只需删除
$saveArr['image'] = $_FILES['image'];
Use below updated code. 使用下面的更新代码。
$add_data['first_name'] = ($this->input->post('first_name') && !empty($this->input->post('first_name'))) ? $this->input->post('first_name') : NULL;
$add_data['last_name'] = ($this->input->post('last_name') && !empty($this->input->post('last_name'))) ? $this->input->post('last_name') : NULL;
$saveArr = [];
if(!empty($this->input->post('first_name'))){
$saveArr['first_name'] = $this->input->post('first_name');
}
if(!empty($this->input->post('last_name'))){
$saveArr['last_name'] = $this->input->post('last_name');
}
if(!file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name']))
{
}
else
{
$filename=time().uniqid(rand()).$_FILES['image']['name'];
move_uploaded_file($_FILES["image"]["tmp_name"], "images/" . $filename);
$saveArr['image']=$filename;
}
$this->db->where('id',$add_data['user_id']);
$query=$this->db->update('users', $saveArr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.