6

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.

ddrossi93
  • 366
  • 2
  • 6
  • 17
  • In codeigniter 4 this error occurs when the updating fields are not mentioned in the property $allowedFields array in model class – rufaidulk Oct 26 '20 at 11:27

6 Answers6

1

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

Mr. ED
  • 11,163
  • 10
  • 51
  • 114
  • now for the post('book_name') is that the column in the table that it looks to be posted under? Or is that the name it's looking for from my form? Or neither... I apologize for all these questions – ddrossi93 May 21 '15 at 16:28
  • book_name would be column name in table. – Mr. ED May 21 '15 at 16:29
  • Cool. and I have $this->input->post('whatever') on my form inputs on my view. I'm guessing I delete those, correct? – ddrossi93 May 21 '15 at 16:34
  • Would not know what your rest of your code looks like so could not tell if you need to add other input in that array that belong to that table you can – Mr. ED May 21 '15 at 16:36
  • and yes I would like to add all those inputs to my database, in the same table. I'm guessing I would get rid of the input posts on my view then just do what you had me do for the book name for the rest of the inputs in the model. Does that sound right? – ddrossi93 May 21 '15 at 16:42
  • You should not use $this->input->post() on views only in controllers and models I woul read more of the user guide. – Mr. ED May 21 '15 at 16:49
  • will do. I appreciate the help – ddrossi93 May 21 '15 at 16:50
1

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.

qwertzman
  • 755
  • 1
  • 9
  • 23
1

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');

0

For Example I want to insert data, so I have to do code like below -

My View File(Login_form.php)

    <label>Username:</label> <input type="text" name="username" value="">

    <label>Password:</label> <input type="password" name="password" value="">

My Model File(Model_login.php)

    class Model_login extends CI_Model { 

    public function insert_entry()
    {

    $data= array(
         'username'=>$this->input->post('username'), // here username and password are database fields.

         'password'=>$this->input->post('password'),

    );

    $this->db->insert('login', $data); //here login is my database table's name 

       }
    }

Controller File(Login.php)

    class Login extends CI_Controller {

    public function index()

    {

      $this->load->view('Login_form'); //to load view file 

    }

    public function getLoginValues()

    {
      $this->load->model('Model_login'); //to load Model file
      $data=$this->Model_login->insert_entry();//to load method of Model file
    }
   }

It works fine, check it.

Alexander Farber
  • 18,345
  • 68
  • 208
  • 375
Ritz
  • 1
  • 1
0

in your model.

public function SetFoo($bar) {
    $data = [
        'YourColumnName'     => 'Yes'
    ];

    // Using Query Builder
    $this->set($data);
    $this->where('id',$bar);
    $this->update();
}

This will throw the above error if you do not add tot he top of the model the following

protected $allowedFields = ['YourColumnName'];

The columns can exist in the model but are not accessible using set update unless its in the allowedfields section (its like a private/public for the code to see the column)

0

the problem is in your model. so you have to check if the array $data is empty or not

like this :

class Add_book extends CI_Model {

public function add_book($data){

    if ( $data != null)
        $this->db->insert('ST_ITM', $data);

}

}

?>
Omar Jribi
  • 41
  • 2