i am developing this login system with admin. i am currently making the model for verifying the usertype, (admin or user) but the query does not work, while its almost the same as the one with account verification. i checked if the post parameter is returned to the second query, and it is, but it is still not working. but the good part is, if i hard coded the username, it works perfectly. please help. here is my code
login_model.php
public function can_log_in(){
$sql ="SELECT * FROM tbl_users WHERE USERNAME = ? AND PASSWORD = ?";
$data = array(
'USERNAME' => $this->input->post('USERNAME'),
'PASSWORD' => $this->input->post('PASSWORD')
);
$query = $this->db->query($sql, $data);
//$result = $query->result_array();
//print_r($result);
//exit;
if ($query->num_rows() == 1){
return true;
}else{
return false;
}
}
public function returnQuery(){
////hard coded (works perfectly)
//$sql ="SELECT USERCODE FROM tbl_users WHERE USERNAME = 'username' AND PASSWORD = 'password'";
//$query = $this->db->query($sql);
//$result = $query->result_array();
///database based.
$sql ="SELECT USERCODE FROM tbl_users WHERE USERNAME = ? AND PASSWORD = ?";
$data = array(
'USERNAME' => $this->input->post('USERNAME'),
'PASSWORD' => $this->input->post('PASSWORD')
);
//print_r($data);exit;
$query = $this->db->query($sql, $data);
//print_r($query);exit; ->> output is an empty array
$result = $query->result_array();
//print_r($result);exit;
if(count($result) > 0){
return $result[0]["USERCODE"];
}else{
return 0;
}
}
try this and please make sure the $this->input->post('USERNAME')
and $this->input->post('PASSWORD')
contains data.
$sql ="SELECT * FROM tbl_users WHERE USERNAME = ? AND PASSWORD = ?";
$data = array(
$this->input->post('USERNAME'),
$this->input->post('PASSWORD')
);
$query = $this->db->query($sql, $data);
The method should be like this
public function can_log_in(){
$sql ="SELECT * FROM tbl_users WHERE USERNAME = ? AND PASSWORD = ?";
$data = array(
$this->input->post('USERNAME'),
$this->input->post('PASSWORD')
);
$query = $this->db->query($sql, $data);
if ($query->num_rows() == 1){
return true;
}else{
return false;
}
}
and your returnQuery
might be like this
public function returnQuery(){
$sql ="SELECT USERCODE FROM tbl_users WHERE USERNAME = ? AND PASSWORD = ?";
$data = array(
$this->input->post('USERNAME'),
$this->input->post('PASSWORD')
);
$query = $this->db->query($sql, $data);
$result = $query->result_array();
if($query->num_rows() > 0){
return $result[0]["USERCODE"];
}else{
return 0;
}
}
Hope that helps.