Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop updating password in codeigniter in edit method?

When i edit a user and save it , it saves blank filed in database. It Save all other fields as it is, but for password i have to manually add password every time i edit a user.

This is my edit 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 a user and i dont change his password i dont want the password updated to be blank instead keep the last inserted passoword.

But My Code generates this query:

UPDATE `users` SET `emp_id` = '21', `name` = 'Rajan', `last_name` = 'Jadav', `email` = '[email protected]', `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

THe Model Code:

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

If I remove the hash method from controller then it inserts blank field and if i keep it it inserts false values

like image 882
Rajan Avatar asked Jan 05 '16 08:01

Rajan


2 Answers

I just found a solution.

I will check if i have blank password if so then will unset it and if not then insert the new password using this:

if(!empty($data['password'])) 
                {
                    $data['password'] = $this->user_m->hash($data['password']);
                } else {
                    // We don't save an empty password
                    unset($data['password']);
                }
like image 117
Rajan Avatar answered Sep 18 '22 01:09

Rajan


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

2.) In your controller add some var_dumps to check for 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's dump the $data array and kill the app:
    var_dump($data);die;

You can move the var_dump successively down step after step to see exactly what the value of your vars is.

Tip: I am guessing the problem is in the View (HTML Form) - But with Profiler and var_dump you should see that very easily.

Hope this helps - Good Luck!

like image 21
Steven M Avatar answered Sep 18 '22 01:09

Steven M