Active records - Assigned lines always returning 1

advertisements

Well, i am trying to make a change password code using the active records. That works fine but i need to know whether the password was successfully changed or that the current password was actually wrong. So, i am using affected rows to see how many records were affected. The value should be 1 or 0, 1 when the password has been successfully changed and 0 when it hasn't changed(that is entered current password was wrong.) The affected rows would never return more than 1 because the username is unique. So it should work the way i am approaching it. But it doesn't seem to work , since the affected rows function is always returning 1.

Here are the Codes Controller:

function changepass()
 {
  $this->form_validation->set_rules('pass', 'Password', 'required|matches[passconf]|min_length[6]|max_length[12]');
  $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        if ($this->form_validation->run() == FALSE)
        {
            //if not valid
   $this->session->set_flashdata('message', validation_errors());
            redirect('profile');
        }
        else
        {
            //if valid
            $current = $this->input->post('current');
            $change = $this->input->post('pass');
   $id = $this->input->post('id');
            $flag=$this->profile_model->update_pass($current,$change,$id);
   if($flag = TRUE)// If Successful
   {
    $this->session->set_flashdata('message', $flag);
   }
   else // If Unsuccessful
   {
    $this->session->set_flashdata('message', 'Current Password is not valid');
   }
            redirect('profile');
        }
 }

Model:

function update_pass($current,$change,$id)
 {
  $data = array('pass'=>$change);
  $this->db->where('id',$id);
  $this->db->where('pass',$current);
  $this->db->update('users',$data);
  return ($this->db->affected_rows());
    }


change your model like this

function update_pass($current,$change,$id)
 {
  $data = array('pass'=>$change);
  $this->db->where('id',$id);
  $this->db->where('pass',$current);
  $this->db->update('users',$data);
if($this->db->affected_rows() > 0)
  return TRUE ;
else
return FALSE;
    }

And change your controller if statement by adding '==' instead of '='

if($flag == TRUE)// If Successful
   {
    $this->session->set_flashdata('message', $flag);
   }