Php 在null上调用成员函数helper()
我正试图找出codeigniter,目前无法插入到我的数据库中。我一直在关注官方文件,并成功地阅读了DB。我的全部错误: 遇到未捕获的异常类型:错误 消息:在null上调用成员函数helper() 文件名: /Library/WebServer/Documents/codeChallenge/system/libraries/Form_validation.php 电话号码:147 回溯: 文件: /Library/WebServer/Documents/codeChallenge/application/models/People\u model.php 行:6函数:\构造 文件: /Library/WebServer/Documents/codeChallenge/application/controllers/People.php 第7行功能:型号 文件:/Library/WebServer/Documents/codeChallenge/index.php行:315 功能:需要一次 解决此错误的解决方案是什么 控制器:Php 在null上调用成员函数helper(),php,database,api,codeigniter,codeigniter-3,Php,Database,Api,Codeigniter,Codeigniter 3,我正试图找出codeigniter,目前无法插入到我的数据库中。我一直在关注官方文件,并成功地阅读了DB。我的全部错误: 遇到未捕获的异常类型:错误 消息:在null上调用成员函数helper() 文件名: /Library/WebServer/Documents/codeChallenge/system/libraries/Form_validation.php 电话号码:147 回溯: 文件: /Library/WebServer/Documents/codeChallenge/applic
class People extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('people_model');
$this->load->helper('url_helper');
}
public function index()
{
$data['people'] = $this->people_model->get_people();
$data['title'] = 'People';
$this->load->view('templates/header', $data);
$this->load->view('people/index', $data);
$this->load->view('templates/footer');
}
public function view($id = NULL)
{
$data['people_item'] = $this->people_model->get_people($id);
if (empty($data['people_item']))
{
show_404();
}
$this->load->view('templates/header', $data);
$this->load->view('people/view', $data);
$this->load->view('templates/footer');
}
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('age', 'Age', 'required');
$this->form_validation->set_rules('city', 'City', 'required');
$this->form_validation->set_rules('province', 'Province', 'required');
$this->form_validation->set_rules('country', 'Country', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('people/create');
$this->load->view('templates/footer');
}
else
{
$this->people_model->set_people();
$this->load->view('people/success');
}
}
}
型号:
<?php
class People_model extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_people($id = FALSE)
{
if ($id === FALSE)
{
$query = $this->db->get('people');
return $query->result_array();
}
$query = $this->db->get_where('people', array('id' => $id));
return $query->row_array();
}
public function set_people()
{
$this->load->helper('url');
$data = array (
'name' => $this->input->post('name'),
'age' => $this->input->post('age'),
'city' => $this->input->post('city'),
'province' => $this->input->post('province'),
'country' => $this->input->post('country')
);
return $this->db->insert('people', $data);
}
}
在您的示例中,
People\u model
是一个模型,但您将其扩展为控制器
更改:
class People_model extends CI_Controller
进入
它说,分析错误消息有帮助(除其他外)
文件:
/Library/WebServer/Documents/codeChallenge/application/models/People\u model.php
行:6函数:\构造
检查
People\u model
会导致错误,将模型扩展为控制器我怎么会错过这么简单的东西!感谢您指出我的错误。如果您仔细阅读错误消息,它实际上会给您一个提示:Backtrace:File:/Library/WebServer/Documents/codeChallenge/application/models/People\u model.php行:6函数:\u构造
$route['people/create'] = 'people/create';
$route['people/(:any)'] = 'people/view/$1';
$route['people'] = 'people';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
class People_model extends CI_Controller
class People_model extends CI_Model