5
nine
4y

So this is the scenario:

A Database Error Occurred: You must use the “set” method to update an entry

This is the code:

$this->db->where('id', $id);

return $this->db->update('contractors', ((array) $contractor));

And it gives me the error A Database Error Occurred: You must use the "set" method to update an entry.

Comments
  • 1
    When your passing an array of column name / value pairs you need to use set() before calling where / update.

    Don't worry, I dislike CI aswell 😅

    @highlight
    $data = array(
    'name' => 'test',
    'email' => 'test@example.com'
    );

    $this->db->set($data)
    $this->db->where('id', $id);
    $this->db->update('users');
  • 0
  • 0
    @C0D4 So I have a function named ‘updateInfo’ where I am trying to update database. I did try the following

    function updateInfo($data)
    {
    $this->db->set($data);
    $this->db->where(‘id’,$id);
    $this->db->update(‘contractors’);
    }

    Still I am getting the same error
  • 0
    @nine what's in $data, and do the array keys match the db column names?

    Got a gist with more code I can look at?
  • 1
    @C0D4 $data is an array of information like name, contact number, etc. The values does match the database columns
  • 0
    @nine odd, that should work.
    Which version of CI are you using?

    Yanking out the old code base here, you should be able to do this:

    do you hand it an id, or is the Id inside $data?

    @highlight
    $id = 1;
    $data = array("name" => "test");
    $this->db->where("id", $id);
    $this->db->update("tableName", $data);

    // which would produce:
    # update tableName set name = "test" where id = 1;

    // alternatively
    $this->db->update("tableName", $data, "id = 1");
  • 0
  • 0
    @C0D4 So, now the update query works in dev and not in prod. Agrhhhh
Add Comment