Sonu Bamniya February 2016

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

Answers


AnkiiG February 2016

You have used

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

but insert_item is a model function.

Load model and then call via model like below :

$this->load->model('Blog');
$this->Blog->get_last_ten_entries();


Tom Wright February 2016

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,
        'm 


Brian Ramsey February 2016

You need to be sure you are calling your model->function correctly. The naming conventions are off between the 2 (they have to be called as they appear in your model) last call in data_submitted() should be: $this->user_model->insert_item($data1); as per below

Controller:

<?php class Pages extends CI_Controller
{

public function __construct()
{
    parent::__construct();
    $this->load->model('user_model');
}

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->db->set($data1);
    $this->user_model->insert_item($data1);
}
} ?>

Model:

<?php class User_model extends CI_Model
{

function __construct()
{
    /* Call the Model constructor */
    parent::__construct();
    $this->load->database();
}

private $item;

function insert_item($item)
{
    /*$this->table = "contact";
    $this->item = $item;
    */
    //$dbconnect = $this->load->database();

    $this->db->insert("contact", $item);
}
}

Post Status

Asked in February 2016
Viewed 3,668 times
Voted 14
Answered 3 times

Search




Leave an answer