Error: You must use the "set" method to update an entry fix? Error: You must use the "set" method to update an entry fix? codeigniter codeigniter

Error: You must use the "set" method to update an entry fix?


You need to do something like this Example Only

class Add_book extends CI_Model {public function add_book(){    // 'book_name' would be the name of your column in database table    $data = array(    'book_title' => $this->input->post('book_title'),    'book_author' => $this->input->post('book_author'),    'book_name' => $this->input->post('book_name')    );    $this->db->set($data);    $this->db->insert($this->db->dbprefix . 'ST_ITM');}}

On view the input would be like example

<input type="text" name="book_name" value="" />

<input type="text" name="book_title" value="" />

<input type="text" name="book_author" value="" />

Best to use a data array in model. and then load model function in success part of form validation Library


To all who arrive here, you do NOT have to use set in your insert queries:

https://www.codeigniter.com/userguide3/database/query_builder.html#inserting-data

$data = array(        'title' => 'My title',        'name' => 'My Name',        'date' => 'My date');$this->db->insert('mytable', $data);// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')

The error is a result of misconstructed insert data (must be an array) or failing to write the correct Active Record query like for example not adding the table name before the insert array.

But sir, when do we use set?

When you need to bypass automatic escaping during updates or replacings for example:

$this->db->set('last_login', 'NOW()', FALSE);$this->db->update(DB_PREFIX .'user', array('login_attempts' => 0));

The third parameter disabled auto-escaping. This gives us the necessary flexibility when writing Active Record queries.


I got this error when i was passing wrong variable into the insert statement.

For example :

function($a){ $this->db->insert($aa); <-- error !!}

So I solved it by passing in the right variable, which is $a.

Also make sure your insertion array is correctly formatted as $a = array('columnname'=>'value');