Search code examples
phpcodeignitercodeigniter-3

Call to undefined method CI_DB_mysqli_driver::insert_item() in D:\wamp\www\registration\application\controllers\pages.php on line 37


I am new to CI and I have just started to code. I am making a simple contact form and got this error.

Call to undefined method CI_DB_mysqli_driver::insert_item() in 
D:\wamp\www\registration\application\controllers\pages.php on line 37

Here is the code of my controller.php

<?php class Pages extends CI_Controller { public function view($page = 'home'){

if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
    // Whoops, we don't have a page for that!
    show_404();
}

$data['title'] = ucfirst($page); // Capitalize the first letter

$this->load->model('user_model');

//$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
//$this->load->view('templates/footer', $data);} public function data_submitted(){
$name = $this->input->get('name');
$email = $this->input->get('email');
$mobile = $this->input->get('mobile');
$address = $this->input->get('address');
$gender = $this->input->get('gender');

$data1 = array(
    'name'=> $name,
    'email'=>$email,
    'mobile'=>$mobile,
    'address'=>$address,
    'gender'=>$gender
);
//$this->db->set($data1);
$this->user_model->insert($data1); } } ?>

and here is full code of my model

<?php class User_model extends CI_Model { private $item; function __construct(){
/* Call the Model constructor */
parent::__construct(); }
function insert_item($item){
/*$this->table = "contact";
$this->item = $item;
*/
//$dbconnect = $this->load->database();
$this->db->insert("contact", $item);} }

Solution

  • Your controller is referencing the db library:

    $this->db->insert_item($data1);
    

    However, the insert_item() method only exists in your model!

    It should look something like this:

        $this->load->model('my_model');
        $this->my_model->insert_item($data1);
    

    Edit here to take into account your controller code.

    Assuming the user_model has already been loaded, and that the user_model contains the method insert_item() you want to change

    $this->user_model->insert($data1);
    

    to

    $this->user_model->insert_item($data1);
    

    Having seen your complete controller and model code...

    Your controller:

    You are loading the user model in your view() method, but from what I can see you're not actually using it. You are however using the model in data_submitted() but it was never loaded there. You were also using insert rather than insert_item(). Here is the corrected code for the controller.

    public function view($page = 'home') {
        if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))
        {
            // Whoops, we don't have a page for that!
            show_404();
        }
    
        $data['title'] = ucfirst($page); // Capitalize the first letter
    
        //$this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        //$this->load->view('templates/footer', $data);
    }
    
    public function data_submitted(){
        $name = $this->input->get('name');
        $email = $this->input->get('email');
        $mobile = $this->input->get('mobile');
        $address = $this->input->get('address');
        $gender = $this->input->get('gender');
    
        $data1 = array(
            'name'=> $name,
            'email'=>$email,
            'mobile'=>$mobile,
            'address'=>$address,
            'gender'=>$gender
        );
    
        $this->load->model('user_model');
        $this->user_model->insert_item($data1);
    }
    

    Your model:

    There seemed to be some code that did absolutely nothing. I have stripped that out and the following code should work.

    class User_model extends CI_Model {
        function __construct(){
            /* Call the Model constructor */
            parent::__construct();
        }
    
    
        public function insert_item($item){
            $this->db->insert("contact", $item);
        }
    }
    

    As a side note - can you see how easy the code is to read now? Make sure you use proper indentation as it massively increases the readability of code.