Wouter
Wouter

Reputation: 149

Codeigniter database update issues

I am having issues in updating a table in my database (MySQL workbench).

The code in my model is the following:

function updateMail($new) {
    $data = array(
        'email' => $new
    );
    $this->db->where('email', $this->session->userdata('email'));
    $result = $this->db->update('person', $data);
    $error = $this->db->error();
    return $error;
}

My controller then places the return value in $result, and chechs if(!isset($result)).

the problem is that sometimes the table updates, sometimes it doesn't, but the error is always set.

The table basically contains persons with an id, name, firstname, password, username, email, and patient field.

Am I doing something wrong? Or is there a way a can display the error message it throws?

Upvotes: 1

Views: 78

Answers (1)

Saty
Saty

Reputation: 22532

Instead of error you have to check number of affected row in your query

function updateMail($new) {
    $data = array(
        'email' => $new
    );
    $this->db->where('email', $this->session->userdata('email'));
    $result = $this->db->update('person', $data);
    $afftectedRows = $this->db->affected_rows();
    if ($afftectedRows > 0) {
        return TRUE;
    } else {
        return FALSE;
    }
}

Upvotes: 1

Related Questions