duanlu0559
2014-10-02 06:46
浏览 98
已采纳

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

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.

Here's my form in first view page - 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:

  <?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:

<?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:

 <?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:

<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:

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. My code syntax was incorrect (most likely in the uses of form and form validation)

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:

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条回答 默认 最新

  • douzao5487 2014-10-02 06:55
    已采纳

    It seems that both:

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

    'admin_logins/employee_add'
    

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

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

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题