Reputation: 2763
I am new to codeigniter. I am trying to insert datat to mysql database to a table named class_record
. My controller add_record.php
coding as below :
class Add_record extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->model('add_record_model');
}
}
And my model add_record_model
is as below :
class add_record_model extends CI_Model{
function __construct(){
parent::__construct();
}
function index(){
$data = array(
'roll_number' => 15,
'student_name' => 'Dhrubajyoti Baishya',
'branch_code' => 'CS'
);
$this->db->insert('class_record',$data);
}
}
But when I type http://localhost/codeigniter/index.php/add_record
in url data are not inserted to the database. What is the problem ?
Upvotes: 0
Views: 897
Reputation: 18685
You're not actually doing anything in the controller and models don't have index functions the way you're thinking.
You want something like this:
class Add_record extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->model('add_record_model');
$this->add_record_model->insertRecords();
}
}
class add_record_model extends CI_Model{
function __construct(){
parent::__construct();
}
function insertRecords(){
$data = array(
'roll_number' => 15,
'student_name' => 'Dhrubajyoti Baishya',
'branch_code' => 'CS'
);
$this->db->insert('class_record',$data);
}
}
The controller does what it says it controls things. By loading the model all you are doing is exposing the models functions to the controller to use directly. Honestly you would pass the data to the model from the controller as well, the function you have is a good little test function but does nada really. How you really want to be doing it is something along these lines.
class Add_record extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$data = array(
'roll_number' => 15,
'student_name' => 'Dhrubajyoti Baishya',
'branch_code' => 'CS'
);
$this->load->model('add_record_model');
$this->add_record_model->insertRecords($data);
}
}
class add_record_model extends CI_Model{
function __construct(){
parent::__construct();
}
function insertRecords($data){
$this->db->insert('class_record',$data);
}
}
Upvotes: 1