查询在代码点火器中不起作用

时间:2016-02-09 08:08:56

标签: php sql codeigniter

我正在使用admin开发此登录系统。我目前正在制作用于验证usertype(管理员或用户)的模型,但查询不起作用,而它与帐户验证的几乎相同。我检查了post参数是否返回到第二个查询,它是,但它仍然无法正常工作。但好的部分是,如果我硬编码用户名,它的工作完美。请帮忙。这是我的代码

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;
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个,请确保$this->input->post('USERNAME')$this->input->post('PASSWORD')包含数据。

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

方法应该是这样的

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;
    }

}

您的returnQuery可能就像这样

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;
    }
}

希望有所帮助。