错误使用CodeIgniter form_validation()函数抛出致命错误

时间:2022-11-24 14:55:19

I'm trying to apply CodeIgniter's form validation feature for an input form in one of my view pages. In that form, all fields are required. If all the fields are successfully filled up and then if the user clicks the submit button, it will redirect to another view page with the list of entries. If any field is left empty, validation messages will be displayed.

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

Here's my form in first view page - employee_add.php:

这是我在第一个视图页面中的表单 - 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>

Here's my controller - employee_adds.php:

这是我的控制器 - 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());

         }
     }
   }
   ?>

Here's controller for master page named admin_logins.php:

这是名为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');

    }
}

Here's my model - employee_model.php:

这是我的模型 - 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);

    }

}
?>

Another view page named employee_list.php is going to load the list of successful entries in its central div:

名为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>

Whenever I try to run the employee_add.php view page, I get the following error:

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

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

From what I'm guessing, it may be due to one of the two following reason:

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

  1. My code designing and layout was incorrect (disorganized coding in views, controller and model)
  2. 我的代码设计和布局不正确(视图,控制器和模型中的无组织编码)

  3. My code syntax was incorrect (most likely in the uses of form and form validation)
  4. 我的代码语法不正确(很可能是在使用表单和表单验证时)

Or maybe, there are other reasons. I just can't figure out why, and exactly where in my code the errors occurred.

或许,还有其他原因。我只是无法弄清楚为什么,以及我的代码中错误发生的确切位置。

EDIT-1: As per user Ghost's solution, I've loaded the form_validation library in my admin_logins/employee method in controller - admin_logins.php. The fatal PHP error's gone, but still the validation is not working. As soon as I click submit button after keeping those fields empty, instead of validation message, I get the following error message:

编辑1:根据用户Ghost的解决方案,我已经在我的admin_logins / employee方法的控制器中加载了form_validation库 - admin_logins.php。致命的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

It seems I still have more syntactical mistakes in my code.

看来我的代码中仍然有更多的语法错误。

1 个解决方案

#1


1  

It seems that both:

似乎两者:

Employee_adds -> index() and Admin_logins ->employee() share the same view file:

Employee_adds - > index()和Admin_logins - > employee()共享相同的视图文件:

'admin_logins/employee_add'

And when you put <?php echo validation_errors(); ?> What triggers the error is that looking on Admin_logins ->employee()

当你把 触发错误的是查看Admin_logins - > employee()

It seems that $this->load->library('form_validation'); is not loaded:

看来$ this-> load-> library('form_validation');未加载:

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

So by the time you access admin_logins/employee, it causes the error since the validation library hasn't been initialized.

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

#1


1  

It seems that both:

似乎两者:

Employee_adds -> index() and Admin_logins ->employee() share the same view file:

Employee_adds - > index()和Admin_logins - > employee()共享相同的视图文件:

'admin_logins/employee_add'

And when you put <?php echo validation_errors(); ?> What triggers the error is that looking on Admin_logins ->employee()

当你把 触发错误的是查看Admin_logins - > employee()

It seems that $this->load->library('form_validation'); is not loaded:

看来$ this-> load-> library('form_validation');未加载:

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

So by the time you access admin_logins/employee, it causes the error since the validation library hasn't been initialized.

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