CodeIgniter PHP致命错误:调用未定义函数form_open()

时间:2018-07-27 22:07:53

标签: php codeigniter codeigniter-3

我对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也不起作用。

1 个答案:

答案 0 :(得分:2)

希望这对您有帮助:

您的代码中出现一系列错字错误,因此请一一删除,然后再次检查

create方法应为Create,将$添加到此处this->studCredModeller->saveRecords();和此处this->load->view('nypportal');

您还缺少url helper中的controller,因此请在url helper中使用controller,而不要在model中使用

注意控制器名称应以大写字母开头,并且必须与文件名匹配,并且最好使用url助手和{{1} } form

中的帮助者

您的控制器autoload.php方法应如下所示:

__construct()

然后在模型中的方法public function __construct() { parent::__construct(); $this->load->library('form_validation'); $this->load->helper('form'); $this->load->helper('url'); this->load->model('studCredModeller'); } 之后删除;,应像这样:

saveRecords();

了解更多:https://www.codeigniter.com/user_guide/general/index.html