user4756836 user4756836 - 1 year ago 70
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'))));

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download