0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Raja
  • 772
  • 1
  • 15
  • 38

4 Answers4

2

Change your home.php controller to this.

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')
            );
        // Transfering Data To Model

        $this->insert_model->insertData($data);
        $this->load->view('home');

    }
  }
}
Ruprit
  • 733
  • 1
  • 6
  • 23
  • Yes, I've changed it and got another error. Please view my post again. I've edited my post. – Raja Feb 06 '15 at 11:41
  • 1
    $autoload['libraries'] = array('database'); check if this is been set in config.php – Ruprit Feb 06 '15 at 11:52
  • $autoload['libraries'] = array('database'); that is all you need to fix "Message: Undefined property: Home::$db" this error. Are you sure you still getting same error? – Ruprit Feb 06 '15 at 12:22
1

@Raj, Okay great you got that fixed. Well it is not need to add this line twice $autoload['libraries'] = array();

You should only keep one line i.e. $autoload['libraries'] = array('database');

Ruprit
  • 733
  • 1
  • 6
  • 23
  • Thank you pritzzz. I understood it after I post my answer. Those variables are sequential I mean, one after another and I should write the code where exactly they are. – Raja Feb 10 '15 at 18:38
0

Set your __construct() outside you index() function in your controller.

AdrienXL
  • 3,008
  • 3
  • 23
  • 39
0

The final thing is I have set $autoload['libraries'] = array('database'); before $autoload['libraries'] = array();. Therefore it was giving error.

Thanks to karan thakkar who found my primary error i.e. constructor error and pritzzz for $autoload['libraries']

My problem has been solved.

Raja
  • 772
  • 1
  • 15
  • 38