1

I am getting data from view using post method ie., in $data variable and I need pass this i.e, $data to model I have tried like below but it is showing like A PHP

Error was encountered Severity: Notice Message: Undefined property: Post::$data Filename: core/Model.php Line Number: 77

Below is mycode:

Array
(
    [name] => Mohan
    [email] => mohan@gmail.com
    [phone] => 9739764966
    [location] => TEST
    [website] => 192.168.1.156
    [useragent] => Chrome
    [ipaddress] => 192.168.1.23
    [desc] => Bengaluru
    [captcha] => 20701449
    [captcha_word] => 20701449
    [submit] => Submit
)

controller:

public function postEnquiry()
{
    $data = $this->input->post();
    // echo "<pre>";print_r($data);die;
    $this->load->model('Enquiry');
    $result = $this->Enquiry->sendenquiry($data);
    if($result){
        $this->session->set_flashdata('success','Sent Successfully');
        redirect(ROOT_PATH."/form");
    }

}

Model:

<?php
class Enquiry extends CI_Model {

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function sendenquiry($data)
    {
        print_r($data);die;
    }

}
?>  
u_mulder
  • 54,101
  • 5
  • 48
  • 64
Moha kumar
  • 35
  • 1
  • 1
  • 11

3 Answers3

0

You must load the db library first in autoload.php

$autoload['libraries'] = array('database');

I hope this will help you

Jay Zamsol
  • 1,173
  • 8
  • 14
0

First, you must define the variable then assign post values to variable and pass the arguments,

public function postEnquiry()
{
    $this->load->model('Enquiry');
    $data = array();
    $data = $this->input->post();       
    $result = $this->Enquiry->sendenquiry($data);
    if($result){
        $this->session->set_flashdata('success','Sent Successfully');
        redirect(ROOT_PATH."/form");
    }
}

(OR)

// You can get post values from model directly
// 1.Controller
public function postEnquiry()
{
    $this->load->model('Enquiry');
    $result = $this->Enquiry->sendenquiry();
    if($result){
        $this->session->set_flashdata('success','Sent Successfully');
        redirect(ROOT_PATH."/form");
    }
}

// 2.Model
public function sendenquiry()
{
  echo '<pre>'; print_r($this->input->post()); echo '</pre>'; //post values
}
0

You need to load your database library in autoload.php:

$autoload['libraries'] = array('database');

Also, load your model in the constructor or inside your function like this:

$this->load->model('User');

Take a look to a similar question here codeigniter model error: Undefined property

Hope it helps.