如何在编辑方法中停止更新codeigniter中的密码?

Raj*_*jan 6 php codeigniter

当我编辑用户并保存它时,它会在数据库中保存空白.它保存所有其他字段,但是对于密码我每次编辑用户时都必须手动添加密码.

这是我的编辑方法::

if ($id) 
        {
            $this->data['user'] = $this->user_m->get_emp($id);

            count($this->data['user']) || $this->data['errors'][] = 'User could not be found';

            $rules = $this->user_m->rules_admin;
            $id || $rules['password']['rules'] .= '|required';
            $this->form_validation->set_rules($rules);

                    // Process the form
            if ($this->form_validation->run() == TRUE) 
            {

                $data = $this->user_m->array_from_post(array('emp_id','name','last_name','email','password','phone','gender','designation','user_type','blood_group','date_birth','status','address'));


                $data['password'] = $this->user_m->hash($data['password']);

                $id = $this->session->userdata('id');


                $this->user_m->save($data, $id);

                redirect('admin/user/index');
            }

        }

        // Load the view
        $this->data['subview'] = 'employee/profile/edit';
        $this->load->view('employee/_layout_main', $this->data);
Run Code Online (Sandbox Code Playgroud)

这是我的HASH方法:

public function hash ($string)
{
    return hash('sha512', $string . config_item('encryption_key'));
}
Run Code Online (Sandbox Code Playgroud)

现在我想要当我编辑用户并且我不更改他的密码我不希望密码更新为空白而是保持最后插入的密码.

但我的代码生成此查询:

UPDATE `users` SET `emp_id` = '21', `name` = 'Rajan', `last_name` = 'Jadav', `email` = 'rajan@bizrtc.com', `password` = '3eee66dbace42d2e671c52013e41de441b176dbaa0f7df33a5811b86c78b60ecb5328184bf1f5057f94817801140d7287f31c1fb06fa65550c356a33a8eec0db', `phone` = '999999999988', `gender` = 'Male', `designation` = 'Web', `user_type` = 'employee', `blood_group` = '+ve', `date_birth` = 'DD-MM-YYYY', `status` = 'Active', `address` = 'DD-MM-YYYY' WHERE `id` = 18
Run Code Online (Sandbox Code Playgroud)

型号代码:

public function save($data, $id = NULL){




    // Set timestamps
    if ($this->_timestamps == TRUE) {
        $now = date('Y-m-d H:i:s');
        $id || $data['created'] = $now;
        $data['modified'] = $now;
    }

    // Insert
    if ($id === NULL) {
        !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
        $this->db->set($data);
        $this->db->insert($this->_table_name);
        $id = $this->db->insert_id();
    }
    // Update
    else {
        $filter = $this->_primary_filter;
        $id = $filter($id);
        $this->db->set($data);
        $this->db->where($this->_primary_key, $id);
        $this->db->update($this->_table_name);
    }

    return $id;
}
Run Code Online (Sandbox Code Playgroud)

如果我从控制器中删除哈希方法,那么它会插入空白字段,如果我保留它,则会插入错误值

Raj*_*jan 4

我刚刚找到了解决方案。

我将检查我的密码是否为空,如果是,则取消设置,如果没有,则使用以下命令插入新密码:

if(!empty($data['password'])) 
                {
                    $data['password'] = $this->user_m->hash($data['password']);
                } else {
                    // We don't save an empty password
                    unset($data['password']);
                }
Run Code Online (Sandbox Code Playgroud)