Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to a member function insert() on a non-object, codeigniter

I am trying to insert data to mysql in codeigniter. Controller class :

class Ci_insert extends CI_Controller
    {
        function __construct()
        {
            parent::__construct();
        }

        function index()
        {
            $data = array(
                          "USN" => "TRE5rCS89G",
                          "name" => "NITISH DOLAKASHARIA",
                          "branch" => "CS"
                          );

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

            $this->ci_insert_model->addToDb($data);
        }

    }

Model Class :

class ci_insert_model extends CI_Model
    {
        function __construct()
        {
            parent::__construct();
        }
        function addToDb($data)
        {

            //var_dump($data);
            $this->db->insert('class_record',$data);
        }
    }

But when I tried to run the code, it shows Fatal error: Call to a member function insert() on a non-object in C:\wamp\www\CodeIgniter\application\models\ci_insert_model.php on line 12.

Whats wrong with the code above?

like image 940
Nitish Avatar asked Dec 18 '25 07:12

Nitish


1 Answers

You're missing $this->load->database();

$this->db->method_name(); will only work when the database library is loaded.

If you plan on using the database throughout your application, I would suggest adding it to your autoload.php in /application/config/.

As others have mentioned, remove the CI_ prefix from your class names. CI_ is reserved for framework classes.

like image 129
Brendan Avatar answered Dec 22 '25 00:12

Brendan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!