遇到PHP错误“已定义的未定义变量”但仍无法正常工作

时间:2015-01-13 08:37:36

标签: php codeigniter variables undefined

我收到了一个错误:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: controllers/c_verifylogin.php

Line Number: 17

这是有错误的文件名:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_verifyLogin extends CI_Controller {
    function __construct() {
        parent::__construct();
        //load session and connect to database
        $this->load->model('m_login','login',TRUE);
        $this->load->helper(array('form', 'url','html'));
        $this->load->library(array('form_validation','session'));
    }

    function index() {
        $this->form_validation->set_rules('studentid', 'studentid', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean|callback_check_database');

        if($this->form_validation->run() == FALSE) {
            $this->load->view('v_login');
            } else {
                //Go to private area
                redirect(base_url('c_home'), 'refresh');
            }       
     }

     function check_database($password) {
         //Field validation succeeded.  Validate against database
         $studentid = $this->input->post('studentid');
         //query the database
         $result = $this->login->login($studentid, $password);
         if($result) {
             $sess_array = array();
             foreach($result as $row) {
                 //create the session
                 $sess_array = array('studentid' => $row->studentid);
                 //set session with value from database
                 $this->session->set_userdata('logged_in', $sess_array);
                 }
          return TRUE;
          } else {
              //if form validate false
              $this->form_validation->set_message('check_database', 'Invalid username or password');
              return FALSE;
          }
      }
}
/* End of file c_verifylogin.php */
/* Location: ./application/controllers/c_verifylogin.php */

这是传递数据的视图

<!DOCTYPE html>
<html>
 <head>
   <title>Simple Login with CodeIgniter</title>
 </head>
 <body>
   <h1>Simple Login with CodeIgniter</h1>
   <?php echo validation_errors(); ?>
   <?php echo form_open('c_verifylogin/index'); 
   echo form_label("StudentID: ");
   echo form_input("studentid");
   echo br();
   echo form_label("Password: ");
   echo form_password("password");
   echo br();
   echo form_submit("","Login");
   echo form_close();
   ?>
 </body>
</html>

我一直在寻找这个问题几个小时,我仍然不知道它有什么问题。有人可以帮忙吗?

3 个答案:

答案 0 :(得分:1)

这里没有定义$ data变量..如果你想传递它,请定义一些值,否则不要通过 $ data来查看电话......

    if($this->form_validation->run() == FALSE) {
        $this->load->view('v_login',$data);
        } else {
            //Go to private area
            redirect(base_url('c_home'), 'refresh');
        }       
 }

答案 1 :(得分:1)

您必须定义$data

 $data = array('something');
 $this->load->view('v_login',$data);

删除$data

$this->load->view('v_login');

答案 2 :(得分:1)

在您的代码中清楚地表明您没有定义$ data变量 因此,您有2个选项可以将$data定义为

之类的数组
$data = array(); 

或只是加载视图而不传递任何变量,如

$this->load->view('v_login');

我希望它会对你有所帮助。