How to select and decode column value in codeigniter query

I have a table for users where the data for a password column is encoded using an encryption library in Codeigniter. Now I want to select the encoded column to decode and compare with the user input password (Login Validation). Here is the code. I inserted this value like this:

$this->db->insert("my_table",Array("username"=>$this->input->post("username"),"password"=>$this->encrypt->encode($this->input->post("password"))));

      

Now I check the input like this:

 $data = $this->db->get("mytable");
    foreach($data as $d){
     if($d["username"] == $this->input->post("username") && $d["password"] == $this->encrypt->decode($this->input->post("password")){
    //success
    break;
    }
}

      

This works so well for me, but I want a shorter and cleaner way to do it. You know, too, for future coding practice. Here's what I've done so far:

$this->db->get_where("my_table",Array($this->encrypt->decode("password")=>$this->input->post("password")));

      

But yes! This returns an error message. The error says:

Unknown column '0' on where clause

      

+3


source to share


2 answers


The problem is you are setting the decoded password as a database column.

Array($this->encrypt->decode("password")=>$this->input->post("password"))

      



What you need to do is more:

Array("password" => $this->encrypt->decode("password"))

      

0


source


First of all, it is NOT recommended to encrypt the password if it can be decrypted !!!

And it is best to encrypt the input and then select in the database



$this->db->select('*')
$yhis->db->from('mytable')
$this->db->where('username', $this->input->post("username"));
$this->db->where('password', $this->encrypt->encode($this->input->post("password")));
$this->db->get();

      

if result ... do login

0


source







All Articles