遇到 PHP 错误 "undefined variable already defined" 但仍然无法正常工作
A PHP Error was encountered "undefined variable already defined" but still not working
我收到一个错误:
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>
我已经搜索这个问题好几个小时了,但我仍然不知道它出了什么问题。有人可以帮忙吗?
这里没有定义$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);
或
移除$data
$this->load->view('v_login');
在你的代码中清楚地表明你没有定义 $data 变量。
所以你有 2 个选项要么将 $data
定义为像
这样的数组
$data = array();
或者只加载视图而不传递任何变量,例如
$this->load->view('v_login');
希望对你有所帮助
我收到一个错误:
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>
我已经搜索这个问题好几个小时了,但我仍然不知道它出了什么问题。有人可以帮忙吗?
这里没有定义$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);
或
移除$data
$this->load->view('v_login');
在你的代码中清楚地表明你没有定义 $data 变量。
所以你有 2 个选项要么将 $data
定义为像
$data = array();
或者只加载视图而不传递任何变量,例如
$this->load->view('v_login');
希望对你有所帮助