Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code Igniter insert MySQL function with database class

I got the data , that are going to be saved into my database using the CI's database class:

$data = array(
    'login' => $this->input->post('login', TRUE),
    'password' => $this->input->post('password', TRUE),
    'email' => $this->input->post('email', TRUE)
);

return $this->db->insert('account', $data);

Now I need to use the MySQL function PASSWORD() to get the password post hash.

I've tried this way:

'password' => "PASSWORD(" . $this->input->post('password', TRUE) . ");

But CI's database class convert it to the following string:

INSERT INTO `accounts` [..] 'PASSWORD("mypassword")'

so as you can see, it won't work since it will save the whole string between '.

Is there any solution for this or I'd have to use the $this->db->query ?

like image 921
Cyclone Avatar asked Mar 06 '26 07:03

Cyclone


1 Answers

You can use the set() method to set your INSERT values, and then pass a third parameter (FALSE) to prevent CodeIgniter from escaping PASSWORD. Something like this:

$this->db->set('login', $this->input->post('login', TRUE));
$this->db->set('password', 'PASSWORD("'.$this->input->post('password', TRUE).'")', FALSE);
$this->db->set('email', $this->input->post('email', TRUE));

return $this->db->insert('account');
like image 185
Colin Brock Avatar answered Mar 07 '26 21:03

Colin Brock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!