我对PHP和CodeIgniter本身还很陌生。我知道当你不加载表单助手时会遇到这个错误。然而,我已经添加了,并且仍然面临这个错误。
请看一下我的编码。
这是我的观点:创建
<?php echo form_open('studCred/Create'); ?>
<?php echo validation_errors(); ?>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" name="username" placeholder="Username">
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" name="email" placeholder="Email">
</div>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password" placeholder="Password">
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Admin No</label>
<input type="text" class="form-control" name="adminNo" placeholder="AdminNo">
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Phone number</label>
<input type="number" class="form-control" name="phone" placeholder="Phone">
</div>
</div>
</div>
<input type="submit" value="Submit" name="save" class="btn btn-skin btn-block btn-lg">
<p class="lead-footer">* We'll contact you by phone & email later</p>
</form>
这是我的控制器:studCred
class studCred extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
this->load->model('studCredModeller');
}
//Register students
public function create() {
//load registration view form
$this->load->view('studCred/Create')
;
//set validation rules
$this->form_validation->set_rules('username', 'Username', 'required|callback_check_username_exists');
$this->form_validation->set_rules('adminNo', 'AdminNo', 'required|callback_check_adminNo_exists');
$this->form_validation->set_rules('email', 'Email', 'required|callback_check_email_exists');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('phone', 'Phone', 'required');
if ($this->form_validation->run() === FALSE){
$this->load->view('studCred/Create');
}
else
{
this->studCredModeller->saveRecords();
this->load->view('nypportal');
}
}
}
型号:studCredModeller
if (!defined('BASEPATH'))
exit ('No direct script access allowed!');
class studCredModeller extends CI_Model
{
public function saveRecords();
{
$this->load->helper('url');
$data = array(
'username' =>$this->input->post('username'),
'admin_no' =>$this->input->post('adminNo'),
'email' =>$this->input->post('email'),
'password' => $this->input->post('password'),
'phone' => $this->input->post('phone'));
return $this->db->insert('stud_login', $data);
}
}
谢谢。将其放入config/autoload.php也不起作用。
希望这将帮助您:
你的代码中有一系列拼写错误,所以逐个删除它们,然后再次检查
create
方法应为Create
,将$
添加到此处this->studCredModeller->saveRecords();
和此处this->load->view('nypportal');
controller
中也缺少url helper
,因此在controller
中使用url helper
,而不是在model
中使用
注意:控制器名称应以大写字母开头,并且必须与文件名匹配,并且在autoload.php
中更好地使用url
助手和form
助手
您的控制器__construct()
方法应该是这样的:
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
$this->load->helper('url');
this->load->model('studCredModeller');
}
并且在您的模型中删除方法saveRecords();
之后的;
,应该是这样的:
public function saveRecords()
{
$this->load->helper('url');
$data = array(
'name' =>$this->input->post('username'),
'admin_no' =>$this->input->post('adminNo'),
'email' =>$this->input->post('email'),
'password' => $this->input->post('password'),
'phone' => $this->input->post('phone')
);
return $this->db->insert('stud_login', $data);
}
了解更多信息:https://www.codeigniter.com/user_guide/general/index.html