我想检查一下用户是否存在于数据库中。我的模型中有一个方法如下:我从发布的数据中创建一个新的user
,插入id
和password
,然后将id
传递给函数检查具有此id
的用户是否存在。但它给出了以下错误:
Severity: Notice Message: Undefined property: Login::$User Filename: controllers/login.php Line Number: 56 Fatal error: Call to a member function user_exists() on a non-object in F:\xamp\htdocs\leoSMS\application\controllers\login.php on line 56 Model name : User
public function user_exists($id){
$this->db->select('id');
$this->db->where('id',$id);
$query = $this ->db->get('user_table');
if ($query->num_rows() > 0)
return TRUE;
else
return FALSE;
}
<?php
class Login extends CI_Controller
{
/**
* load the magazines
*/
function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->model('User','National_Holiday','Updated_Holiday');
}
public function index() {
$this->load->view('login');
}
/**
* add a magazine
*/
public function login()
{
$this->form_validation->set_rules(array (
array(
'field' => 'username',
'label' => 'username',
'rules' => 'required|is_numeric|max_length[1]|min_length[1]',
) ,
array(
'field' => 'password',
'label' => 'password',
'rules' => 'required|is_numeric|max_length[1]|min_length[1]',
),
));
$this -> form_validation ->set_error_delimiters(
'<div class="alert alert-error">','</div>');
echo $this->input->post('username') . "<br>";
echo $this->input->post('password'). "<br>";
$user = new User();
$user->user_id = $this->input->post('username');
$user->password = $this->input->post('password');
echo "<tt><pre>". var_dump($user) . "</tt></pre>";
if($this->User->user_exists($user->user_id))
echo "true";
else {
echo "false";
}
if(!$this->form_validation->run()){
$this->load->view('login');
}
else {
$this->load->view('national_holiday_screen');
}
}
}
答案 0 :(得分:0)
尝试使用if($user->user_exists($user->user_id))
代替if($this->User->user_exists($user->user_id))
您的声明正在尝试执行未知属性User的方法。可能用途未定义为Login类的属性。 新语句调用User类对象的方法。存在此对象是因为您刚刚创建了它。
答案 1 :(得分:0)
试试这个模型类
function create_member()/** function to create the new user **/
{
$this->db->where('Username ', $this->input->post('Username '));
$query = $this->db->get('register');
if($query->num_rows > 0)
{
echo '<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>';
echo "Username already taken";
echo '</strong></div>';
}
}