0

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

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Yes I know but that is another question.
1

Define array outside of this statement.

$arr = array( 'email' => $this->input->post('email'), );

Only push the 'password' key into the array if new password is entered.

if($this->input->post('newpwd') !== "") { $arrName = array_merge($arr, array('Password' => md5($this->input->post('newpwd')))); }

Then provide the array to update statement like following statement

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.