[英]Codeigniter check if user exists in database
我想检查用户是否存在于数据库中。 我的模型中有一个方法如下:我创建一个新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');
}
}
}
尝试使用if($user->user_exists($user->user_id))
代替if($this->User->user_exists($user->user_id))
您的语句正在尝试执行未知属性User的方法。 用途可能未定义为Login类的属性。 新语句正在调用User类的对象的方法。 该对象存在是因为您刚刚创建它。
试试这个模型课
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>';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.