【问题标题】:Update query make record empty from MySQL in Codeigniter PHP更新查询使 Codeigniter PHP 中 MySQL 的记录为空
【发布时间】:2019-12-13 17:19:44
【问题描述】:

我正在尝试使用 Codeigniter 从 MySQL 更新记录(first_name,last_name) 但如果我不更新 first_name 或 last_name 那么记录从 MySQL 变为空, 我希望现有记录不应该被删除/删除,我该怎么做?这是我的代码

$add_data['user_id'] = ($this->input->post('user_id') && !empty($this->input->post('user_id'))) ? $this->input->post('user_id') : NULL;     
$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;     
$data = array(
        'first_name'=>$add_data['first_name'],
        'last_name'=>$add_data['last_name'],
        );  
$this->db->where('id',$add_data['user_id']);
$query=$this->db->update('users', $data);

【问题讨论】:

    标签: php mysql rest codeigniter image-uploading


    【解决方案1】:

    这里是sn-p:

      $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');
      }
      $this->db->where('id',$add_data['user_id']);
      $query=$this->db->update('users',  $saveArr);
    

    希望对你有帮助:)

    【讨论】:

    • 在图像情况下呢?如果我不想更新我的图像呢?
    • 另一种情况if(!empty($_FILES['image']['name'])) { ....... }
    猜你喜欢
    • 2020-07-19
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多