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

ddrossi93 picture ddrossi93 · May 21, 2015 · Viewed 58.1k times · Source

I am using codeigniter as my PHP framework, and I keep getting this error when I submit my from to post to my database.

You must use the "set" method to update an entry

I am not exactly sure what that means, from the other posts I have looked at, everyone says that the datamapper needs to have a value assigned to the object.

Being that I am new to all this, could someone give me a better explaniation.

Here is my code where it says I have the error:

class Add_book extends CI_Model {

public function add_book($data){

    $this->db->insert('ST_ITM', $data);

}

}

?>

Thank you.

Answer

Mr. ED picture Mr. ED · May 21, 2015

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