0

I am trying to insert data into a table named class_record in a MySQL database. My controller class aadd_record is written in my add_record.php file.

class Add_record extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }
        
    function index()
    {
        $this->load->model('add_record_model');
    }
}

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 the url, data is not inserted into the database. What is the problem?

3
  • When you open the said URL, does the page open or no page appears at all? Maybe the problem is with URL Commented Oct 31, 2012 at 1:59
  • Are you sure your code is run? What if you put die('foo bar'); in the model's index method and see if it's output? Commented Oct 31, 2012 at 2:01
  • OK..maybe the url problem ! I put die('foo bar'); in the model's index and the page is blank !. what can be the problem? sorry I am only 3rd day to CI Commented Oct 31, 2012 at 2:04

1 Answer 1

1

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);
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much Rick Calder. Special thanks for the explanation. Helped me a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.