我使用post方法从视图中获取数据,即$ data变量 我需要通过这个,即$ data to model我试过如下,但它显示为A PHP
遇到错误严重性:通知消息:未定义属性: Post :: $ data文件名:core / Model.php行号:77
以下是我的代码:
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
)
控制器:
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");
}
}
型号:
<?php
class Enquiry extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function sendenquiry($data)
{
print_r($data);die;
}
}
?>
答案 0 :(得分:0)
您必须先在 autoload.php
中加载数据库库$autoload['libraries'] = array('database');
我希望这会对你有所帮助
答案 1 :(得分:0)
首先,您必须定义变量,然后将post值赋给变量并传递参数
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
}
答案 2 :(得分:0)
您需要在autoload.php中加载数据库:
$autoload['libraries'] = array('database');
此外,在构造函数或函数内部加载模型,如下所示:
$this->load->model('User');
在这里查看类似的问题codeigniter model error: Undefined property
希望它有所帮助。