Search code examples
phpcodeignitercart

Severity: Notice Trying to get property of non-object and did not get any data from database table codeigniter


I am doing a project in codeigniter.Here i got an error 'Trying to get property of non-object codeigniter'. I have tried some code given below,I am a newbie trying to learn code,therefore please help me in this regard. controller code:-

class Cart extends CI_Controller{
    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('Cart_model');
        $this->load->library('cart');

    }
public function add_to_cart(){
      $product_id= $this->input->post('product_id',true);
      $category_info= $this->Cart_model->select_product_info_by_product_id($product_id);
      $data = array(
        'id'      =>$category_info->product_id,
        'price'   => $category_info->product_price,
        'name'    => $category_info->product_name,
        'options' => array('image' => $category_info->product_image)
);
echo '<pre>';
print_r($data);
exit();
$this->cart->insert($data);
$this->load->view('Cart/show_cart');

    }
}

Model code:-

class Cart_model extends CI_Model{
    //put your code here
    public function select_product_info_by_product_id($product_id) {
      $product_info= $this->db->select('*')
              ->from('tbl_product')
      ->where('product_id',$product_id)
      ->get()->row();
      return $product_info;
    } 
}

whenever i run the code i get an error like this:-

Severity: Notice Message: Trying to get property 'product_id' of non-object

Severity: Notice Message: Trying to get property 'product_price' of non-object

Severity: Notice Message: Trying to get property 'product_name' of non-object

Severity: Notice Message: Trying to get property 'product_image' of non-object

And also I didn't get any value in the array.

Array
(
    [id] => 
    [price] => 
    [name] => 
    [options] => Array
        (
            [image] => 
        )

)

Solution

  • I did not get product_id.I forgot to name the 'product_id' in my view page.When i placed the name='product_id' in view page, this actually solved the problem.