user4756836 user4756836 - 1 month ago 10
PHP Question

If statement inside array php

I am trying to update the database with new

newpwd
only if the new password is entered... Otherwise, i would like it to leave it as it was...

Currently, I am using a workaround and setting it as the
password
field which is the current password on
else
. So my question is, is there a way for me to avoid using an else statement? I don't want to update the
password
row in the database if the new password is not entered.

My code:

$this->db->update("userinfo", array(
'email' => $this->input->post('email'),
'password' => ($this->input->post('newpwd') !== "") ? md5($this->input->post('newpwd')) : md5($this->input->post('password'))));

Answer

You need to add it to array conditionally, like following code:

$userData['email'] = $this->input->post('email');
if ($this->input->post('newpwd') !== "") { 
    $userdata['password'] = md5($this->input->post('newpwd'));
}

$this->db->update("userinfo", $userData);