I want to save data from a form in Codeigniter.
Controller : home.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('insert_model');
}
public function index(){
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('custName', 'Customer Name', 'required|min_length[1]|max_length[50]');
$this->form_validation->set_rules('custPhone', 'Contact No.', 'required|min_length[1]|max_length[50]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('home');
} else {
$data = array(
'custName' => $this->input->post('custName'),
'custPhone' => $this->input->post('custPhone')
);
// Transferring Data To Model
$this->insert_model->insertData($data);
$this->load->view('home');
}
}
}
Model: insert_model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Insert_model extends CI_Model{
function __construct(){
parent::__construct();
}
function insertData($data){
$this->db->insert('branches', $data);
}
}
View: home.php
<div id="container">
<?php echo form_open('home'); ?>
<h1>Save Data</h1>
<?php echo form_label('Customer Name :'); ?> <?php echo form_error('custName'); ?>
<?php echo form_input(array('id' => 'custName', 'name' => 'custName')); ?><br />
<?php echo form_label('Contact No. :'); ?> <?php echo form_error('custPhone'); ?>
<?php echo form_input(array('id' => 'custPhone', 'name' => 'custPhone')); ?><br />
<?php echo form_submit(array('id' => 'submit', 'value' => 'Save'));?>
<?php echo form_close(); ?>
</div>
When I run it, it gives error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Home::$db
Filename: core/Model.php
Line Number: 52
WAMP Server gives Fatal error: Call to a member function insert() on a non-object in C:\wamp\www\code\admin\application\models\insert_model.php on line 11
This error is in my insert_model ($this->db->insert('branches', $data);)
.
What should I do ? How can I change CI Core
files ?
Note: I found this solution, but the error persists.
Please do not make this post duplicate as I tried several times searching Stackoverflow
and implementing in my code, but failed.