我对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也不起作用。
发布于 2018-07-28 09:46:40
希望这能对你有所帮助:
您的代码中有一系列拼写错误,因此请逐一删除它们,然后再次检查
create
方法应为Create
,将$
添加到此处的this->studCredModeller->saveRecords();
和此处的this->load->view('nypportal');
controller
中也缺少url helper
,因此请在controller
中使用url helper
,而不要在model
中使用
注意:控制器名称应以大写字母开头,且必须与文件名匹配,最好在autoload.php
中使用url
helper和form
helper
您的控制器__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
https://stackoverflow.com/questions/51566122
复制相似问题