Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
遇到一个PHP错误";未定义变量已定义";但仍然不起作用_Php_Codeigniter_Variables_Undefined - Fatal编程技术网

遇到一个PHP错误";未定义变量已定义";但仍然不起作用

遇到一个PHP错误";未定义变量已定义";但仍然不起作用,php,codeigniter,variables,undefined,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 ex

我收到一个错误:

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 */
这是有错误的文件名:

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 */


我已经寻找这个问题好几个小时了,但我仍然不知道它出了什么问题。有人能帮忙吗?

$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');
        }       
 }

您必须定义
$data

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

删除
$data

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

在您的代码中,这里清楚地表明您没有定义$data变量。
因此,您有两个选项:将
$data
定义为类似数组的

$data = array(); 
或者只加载视图而不传递任何变量,如

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

我希望它能帮助您。

确保在
$this->load->view('v_login',$data)行之前定义了
$data
。只是
var_dump($data);模具()要查看它包含的内容,$data在哪里定义?它清楚地表明它不是定义的,所以只需定义它并解决问题。这肯定会帮助您将来解决此类问题。