如果声明里面有数组php

时间:2016-10-14 02:20:11

标签: php arrays function codeigniter if-statement

我只是在输入新密码的情况下尝试使用新的newpwd更新数据库...否则,我希望它保留原样...

目前,我正在使用解决方法并将其设置为password字段,该字段是else上的当前密码。所以我的问题是,有没有办法让我避免使用else语句?如果未输入新密码,我不想更新数据库中的password行。

我的代码:

$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 个答案:

答案 0 :(得分:2)

您需要有条件地将其添加到数组中,如下面的代码:

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

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

答案 1 :(得分:1)

在此语句之外定义数组。

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

如果输入新密码,则只将'密码'键推入数组。

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

然后提供数组以更新语句,如下面的语句

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