How to stop password update in codeigniter in editing method?

When I edit a user and save it, it saves an empty file in the database. It saves all other fields as is, but for the password I have to manually add the password every time I edit the user.

This is my editing method ::

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

This is my HASH METHOD:

 public function hash ($string) { return hash('sha512', $string . config_item('encryption_key')); } 

Now I want that when I edit the user, and I do not change his password, I do not want the updated password to be empty, but to keep the last inserted passive.

But my code generates this request:

 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 

Model Code:

 public function save($data, $id = NULL){ // Set timestamps if ($this->_timestamps == TRUE) { $now = date('Ymd 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; } 

If I remove the hash method from the controller, then it inserts an empty field, and if I save it, it inserts false values

+6
source share
3 answers

I have found a solution.

I will check if I have an empty password, if so, it will disable it, and if not, then insert a new password using the following:

 if(!empty($data['password'])) { $data['password'] = $this->user_m->hash($data['password']); } else { // We don't save an empty password unset($data['password']); } 
+4
source

1.) Add $this->output->enable_profiler(TRUE); in your code to include Debug Profiling as described here

2.) In your controller, add a few var_dumps to check the values โ€‹โ€‹of your vars:

  $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')); // Let dump the $data array and kill the app: var_dump($data);die; 

You can move the var_dump sequential step after the step to see exactly what your var value is.

Tip. I assume that the problem is in the presentation (HTML form). But with Profiler and var_dump you should see this very easily.

Hope this helps - Good luck!

+2
source

A bad approach is to disconnect the password from your data before updating. In your code, it will look like this:

 unset($data['password']); 

Also, if you do not want to update the password when editing the user, just do not show this field.

+1
source

All Articles