doukan6564 2019-08-07 10:17
浏览 61
已采纳

如果不更新图像codeigniter Php,则图像列为null

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

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);
  • 写回答

2条回答 默认 最新

  • douyu5775 2019-08-07 10:55
    关注

    In both condition you set $saveArr['image'], you just need to set $saveArr['image'] when upload new image. so just remove line $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • duanbiaojin8860 2019-08-07 10:25
    关注

    From what I understand (I don't do PHP or CodeIgniter) your code always specifies the image column so it will always be updated. You need logic that will only update that column if you have a value.

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 q从常量变成sin函数,怎么改写python代码?
  • ¥15 图论编程问题,有可以指导的吗
  • ¥15 DEA的CCR模型画图
  • ¥15 请假各位一个关于安卓车机的问题
  • ¥15 光谱仪怎么看这样的透射谱
  • ¥15 pyqt5 如何实现输入框输入关键词,下拉框显示模糊查询返回的结果?
  • ¥20 fluent模拟,可以燃烧和相变同时模拟吗?
  • ¥50 海康摄像头,C#如何识别车牌号码和抓取JPG
  • ¥15 yolov5 pt转engine的问题
  • ¥15 一公司的网络工程设计