【问题标题】:Incorrect use of the CodeIgniter form_validation() function throwing fatal error错误使用 CodeIgniter form_validation() 函数抛出致命错误
【发布时间】:2015-10-13 16:03:57
【问题描述】:

我正在尝试将 CodeIgniter 的表单验证功能应用于我的一个视图页面中的输入表单。在该表单中,所有字段都是必需的。如果所有字段都成功填写,然后如果用户单击提交按钮,它将重定向到具有条目列表的另一个视图页面。如果任何字段留空,将显示验证消息。

这是我在第一个视图页面中的表单 - employee_add.php:

   <div id="content" class="box">
        <form action="<?php echo base_url();?>index.php/employee_adds/insert_employee_db" method="post">
        <?php echo validation_errors(); ?>
            <fieldset>
                <legend id="add_employee_legend">Add New Employee</legend>
                <div>
                    <label id= "emp_id_add_label">Employee ID:</label>
                    <input type="text" name="emp_id" id = "employee_id_add" placeholder="Employee ID"/>
                </div>
                <div>
                    <label id= "emp_name_add_label">Employee Name:</label>
                    <input type="text" name="emp_name" id = "employee_name_add" placeholder="Employee Name"/>
                </div>
                <input type="submit" name="submit" id = "employee_info_submit" value="Send"/>
            </fieldset>    
        </form>

    </div>

这是我的控制器 - employee_adds.php:

  <?php
  if ( ! defined('BASEPATH')){ exit('No direct script access allowed');}
  class Employee_adds extends CI_Controller
  {
      function __construct()
     {
         parent::__construct();
         #$this->load->helper('url');
         $this->load->model('employee_model');
     }

     //Show all Employees or validate employees
     public function index()
     {
         $this->load->helper(array('form', 'url'));

         $this->load->library('form_validation');

         $this->form_validation->set_rules('emp_id', 'Employee ID', 'required');
         $this->form_validation->set_rules('emp_name', 'Employee Name', 'required');

         if ($this->form_validation->run() == FALSE)
         {
             $this->load->view('admin_logins/employee_add');
         }
         else
         {
             $data['employee_list'] = $this->employee_model->get_all_employees();
             $this->load->view('admin_logins/employee_list');
         }
     }

     //Insert an employee
     public function insert_employee_db()
     {
         $edata['emp_id'] = $this->input->post('emp_id');
         $edata['emp_name'] = $this->input->post('emp_name');
         $res = $this->employee_model->insert_employee($edata);
         if($res)
         {
             header('location:'.base_url()."index.php/employee/".$this->index());

         }
     }
   }
   ?>

这里是名为 admin_logins.php 的母版页控制器:

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin_logins extends CI_Controller 
{
    function __construct()
    {
        parent::__construct();

    }

    public function index()
    {
        $this->load->view('admin_logins/index');
    }

    function employee()
    {

        $this->load->view('admin_logins/employee_add');

    }
}

这是我的模型 - employee_model.php:

 <?php
 class Employee_model extends CI_Model 
 {

    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }   

    //To retrieve all employees
    public function get_all_employees()
    {

        $query = $this->db->get('employee');
        return $query->result();
    }


    //To add a new employee to the database
    public function insert_employee($data)
    {
        return $this->db->insert('employee', $data);

    }

}
?>

另一个名为 employee_list.php 的视图页面将在其中心 div 中加载成功条目列表:

<div id="content" class="box">
            <table id = "employee_list_table" width="600">
                <tr>
                    <th scope="col">Employee Id</th>
                    <th scope="col">Employee Name</th>
                </tr>

                <?php foreach ($employee_list as $e_key){ ?>
                <tr>
                    <td><?php echo $e_key->emp_id; ?></td>
                    <td><?php echo $e_key->emp_name; ?></td>
                </tr>
                <?php }?>
            </table>
        </div>

每当我尝试运行employee_add.php 查看页面时,都会收到以下错误:

Fatal error: Call to undefined function validation_errors() in C:\wamp\www\CI\application\views\admin_logins\employee_add.php

据我猜测,可能是以下两个原因之一:

  1. 我的代码设计和布局不正确(视图、控制器和模型中的编码杂乱无章)
  2. 我的代码语法不正确(很可能是在使用表单和表单验证时)

或者,也许还有其他原因。我只是想不通为什么,以及我的代码中发生错误的确切位置哪里

EDIT-1:根据用户Ghost 的解决方案,我已在控制器admin_logins.phpadmin_logins/employee 方法中加载了form_validation 库。致命的 PHP 错误消失了,但验证仍然无法正常工作。在将这些字段保留为空后单击提交按钮时,我会收到以下错误消息,而不是验证消息:

A Database Error Occurred
Error Number: 1146
La table 'ci_test.employee' n'existe pas
INSERT INTO `employee` (`emp_id`, `emp_name`) VALUES ('', '')
Filename: C:\wamp\www\CI\system\database\DB_driver.php

我的代码中似乎还有更多的语法错误。

【问题讨论】:

    标签: php html mysql codeigniter


    【解决方案1】:

    似乎两者都有:

    Employee_adds -&gt; index()Admin_logins -&gt;employee() 共享同一个视图文件:

    'admin_logins/employee_add'
    

    当您输入&lt;?php echo validation_errors(); ?&gt; 时,触发错误的是查看Admin_logins -&gt;employee()

    好像$this-&gt;load-&gt;library('form_validation');没有加载:

    function employee()
    {
        $this->load->view('admin_logins/employee_add');
    }
    

    所以当您访问admin_logins/employee 时,它会导致错误,因为尚未初始化验证库。

    【讨论】:

    • 那么,我也应该在Admin_logins -&gt;employee() 中添加$this-&gt;load-&gt;library('form_validation'); 吗?
    • @CrescentMoon 是的,因为在 admin_logins/employee 的情况下,如果验证库甚至没有加载,则拥有 &lt;?php echo validation_errors(); ?&gt; 是没有意义的。
    • 我在admin_logins/employee 方法中添加了库。现在致命错误消失了,但验证无法正常工作。如果我将这些字段留空并提交,那么我会收到数据库错误,而这意味着提供验证。
    • @CrescentMoon 那么你应该把Employee_adds/index 的代码也放到admin_logins/employee 中,因为你的规则是正确的?
    • @CrescentMoon 我认为您应该在该管理控制器上设置另一个进程,因为您的表单仍在使用另一个控制器内的进程:&lt;form action="&lt;?php echo base_url();?&gt;index.php/employee_adds/insert_employee_db" method="post"&gt;
    猜你喜欢
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    相关资源
    最近更新 更多