user4756836 user4756836 - 3 months ago 15
PHP Question

If statement inside array php

I am trying to update the database with new

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
field which is the current password on
. So my question is, is there a way for me to avoid using an else statement? I don't want to update the
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'))));


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