CodeIgniter -Error Call member function () on non-object

I am creating a registration. Whenever I try to insert data into the db it throws this error.

A PHP error has occurred. Severity: Notice: Undefined property: insert_ctrl :: $ insert_model Filename: controllers / insert _ctrl.php Line Number: 38 Fatal Error: Call member function form_insert () on non-object in C: \ xampp \ htdocs \ NewProject \ application \ controllers \ insert_ctrl.php on line 38

This is my insert_ctrl.php

<?php

class insert_ctrl extends  CI_Controller{

    function _construct()
    {
        parent :: _construct();
        $this->insert_model->form_insert($data);

    }

    function index()
    {
        //Including validation library
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class ="error">','</div>');

        //Validating name field
        $this->form_validation->set_rules('dname','Username','required|min_length[5]|max_length[15]');
        $this->form_validation->set_rules('demail','Email','required|valid_email');
        $this->form_validation->set_rules('dmobile','Mobile No.');
            $this->form_validation->set_rules('daddress','Adress','required|min_length[10]|max_length[50]');

            if($this->form_validation->run()==FALSE)
            {
                $this->load->view('insert_view');
            }
            else {
                //Setting values for db table
                $data=array(
                    'Student_Name'=>$this->input->post('dname'),
                    'Student_Email'=>$this->input->post('demail'),
                    'Student_Mobile'=>$this->input->post('dmoblie'),
                    'Student_Address'=>$this->input->post('daddress')

                );
                //Transfering data to model
                $this->insert_model->form_insert($data);
                //loading view
                $this->load->view('insert_view');
            }
        }
    }

//insert_model.php

<?php
class insert_model extends  CI_Model{
    function _construct()
    {
        parent :: _construct();


    }

    function form_insert($data)
    {
        $this->db->insert('student',$data);
    }
}
?>

      

+3


source to share


4 answers


My controller is insert_ctrl.php

class insert_ctrl extends  CI_Controller{

function __construct()
{
    parent :: __construct();
    $this->load->model('insert_model');

}

function index()
{

        $this->insert_model->form_insert();
        echo "Clear";


}

      

}

My Model- insert_model.php



class insert_model extends  CI_Model{
    function __construct()
    {
        parent :: __construct();


    }

    function form_insert()
    {
        echo "model loaded Successfully";
    }
}

      

This model and controller are named the same as yours. Check how the model was loaded and called.

You can overcome the memory limit error by adding the following line of code on top of your php file.

ini_set ("memory_limit", "512M");

+2


source


You are calling the wrong model function:

You call $this->load->model('Insert_model');

      

This means your model is Insert_model

instead of this:

 $this->insert_model->form_insert($data);

      



You're using

$this->Insert_model->form_insert($data);

      

Change the constructor code to

function _construct()
    {
        parent :: _construct();
        $this->load->model('insert_model');

    }

      

+2


source


Change this line

$ autoload ['libraries'] = array ();

to

$ autoload ['libraries'] = array ('database');

in config\autoload.php

0


source


Add this line: $this->load->model('modelName');

function __construct()
{
    parent :: __construct();
    $this->load->model('modelName');

}

      

-1


source







All Articles