我想更新我的密码字段仅当输入时输入密码(非空),但同时“用户名”可以在表格中更新。
这是我目前的PHP代码:
UPDATE `my_tbl`
SET `username`= '".$dataArray['username']."', `password` = IF( IS NULL('".$dataArray['password']."'),
`password`, '".$dataArray['password']."')
WHERE `id` = ".$dataArray['id']
答案 0 :(得分:1)
使用连接来解决此问题
$sql= UPDATE `my_tbl` SET `username`= '".$dataArray['username']."',
`password` = IF( IS NULL('".$dataArray['password']."'), `password`, '".$dataArray['password']."')
WHERE `id` = ".$dataArray['id'];
将此查询更改为
$condition="";
if($dataArray['password'] != ""){
$condition=" , `password` = '".$dataArray['password']."'";
}
$sql= "UPDATE `my_tbl` SET `username`= '".$dataArray['username'] ."' ".$condition."
WHERE `id` = ".$dataArray['id'];
echo $sql;
答案 1 :(得分:1)
if($dataArray['password'])
{
$condition=" , `password` = '".$dataArray['password']."'"; } else
{
$condition="";
}
$sql= "UPDATE `my_tbl` SET `username`= '".$dataArray['username'] ."' ".$condition." WHERE `id` = ".$dataArray['id'];
答案 2 :(得分:1)
使用codeigniter活动记录:
$data['username'] = $dataArray['username'];
if(!empty($dataArray['password'])) {
$data['password'] = $dataArray['password'];
}
$this->db->where('id', $dataArray['id']);
$this->db->update('my_tbl',$data);