Jack23
Jack23

Reputation: 1396

Update values: not change the value if the field is empty

I'm usign Codeigniter Restful and I would to do an update method.

My problem is that I try to do an update, without checking if the values'field are empty the update deletes the empty fields.

So I have tried to control if the field is empty before update and the result is the I receive this error: You must use the "set" method to update an entry.

How can I do to update values in the db and not delete the empty fields?

This is the controller

public function person_patch()
    {
        $id = $this->uri->segment(3);

        $data = array();

        if(($this->input->post('fiscalcode') != '')){
        $data['fiscalcode']=$this->input->post('fiscalcode');}
        if(($this->input->post('name') != '' )){
        $data['name']=$this->input->post('name');}

        var_dump($_POST);
        $this->load->model('Person_model');
        $query = $this->Person_model->patch($id, $data);
}

This is the model:

public function patch($id, $data)
    {
        $result = $this->db->update('Persons', $data, array('id' => $id));
        return $result;
    }

So for example if I pass using a form only a field (like fiscalcode) now I receive the error. If I don't check is a field is empty, the field name become empty.

Upvotes: 0

Views: 298

Answers (1)

Dum
Dum

Reputation: 1501

I guess you only receive ther error when both fields are empty. Try following.

if(!empty($data)){
   $this->load->model('Person_model');
   $query = $this->Person_model->patch($id, $data);
}

Upvotes: 1

Related Questions