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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With