2017-07-17 68 views
0

#表名是用戶#需要更新用戶表密碼字段上笨

##型號名稱是user_model ##

###控制器的名字是get_password ###

問題 - 對密碼沒有改變,仍然爲老

> model(user_model) 



public function updatePassword($email,$data) 

{ 
     $data1=array('password'=>$data); 

     $this->db->where('email','$email'); 

     $this->db->update('users','password'); 

    $success = $this->db->affected_rows(); 

    if(!$success){ 
     error_log('Unable to updatePassword'); 
     return false; 
    }   
    return true; 
} 


> controller(get_password) 

public function index($rs=FALSE) 
{ 
$this->load->database(); 
$this->load->helper(array('form', 'url')); 
$this->load->library('form_validation'); 
$this->load->model('user_model'); 
$this->load->library('session'); 

    $this->form_validation->set_rules('email', 'Email', 'required|valid_email'); 
    $this->form_validation->set_rules('password', 'password', 'required'); 
    $this->form_validation->set_rules('passconf', 'password Confirmation', 'required|matches[password]'); 

if ($this->form_validation->run() == FALSE) 
    { 
       echo form_open(); 
       $this->load->view('users/fpre'); 
} 
else 
{ 


    $data = array(
     'password' => md5($this->input->post('password')), 


); 

    $email =array(

     'email' =>$this->input->post('email') 
); 



    $this->user_model->updatePassword($data,$email); 

    echo "Congratulations!"; 
    } 

} 

,也沒有任何錯誤,但密碼未更新,在用戶保持相同table..i找不到問題,請幫我找我t出..

+0

不要使用MD5口令可以得到砍死你應該使用PHP的密碼哈希http://php.net/manual/en/function.password-hash.php密碼列在數據庫varchar 255和驗證http://php.net/manual/en/function.password-verify.php – user4419336

回答

1

控制器(get_password):

public function index() { 
     $this->load->database(); 
     $this->load->helper(array('form', 'url')); 
     $this->load->library(array('form_validation', 'session')); 
     $this->load->model('user_model'); 

     $this->form_validation->set_rules('email', 'Email', 'required|valid_email'); 
     $this->form_validation->set_rules('password', 'Current password', 'required'); 
     $this->form_validation->set_rules('newpassword', 'password', 'required'); 
     $this->form_validation->set_rules('newpassconf', 'password Confirmation', 'required|matches[newpassword]'); 

     $email = $this->input->post('email'); 

     $success = false; 
     $msg = ''; 

     if ($this->form_validation->run() !== FALSE) { 
      $password = md5($this->input->post('password')); 

      if ($this->user_model->checkPassword($email, $password)){ 
       $newpassword = md5($this->input->post('newpassword')); 

       if ($this->user_model->updatePassword($email, $newpassword)){ 
        $success = true;      
       }else{ 
        $msg = 'Unable to updatePassword'; 
       }    
      }else{ 
       $msg = 'Incorrect password'; 
      } 
     } 

     if ($success){ 
      echo 'Congratulations!';   
     }else{   
      $this->load->view('users/fpre', array(
       'email'=>$email, 
       'msg'=>$msg 
      )); 
     } 
    } 

模型(user_model):

public function checkPassword($email, $password) { 
    $users = $this->db->get_where('users', array('email'=>$email))->row(); 

    return $users->password === $password; 
} 

public function updatePassword($email, $newpassword) {  
    $data = array('password' => $newpassword); 

    $this->db->where('email', $email) 
    ->update('users', $data); 

    $success = $this->db->affected_rows(); 

    if (!$success) { 
     error_log('Unable to updatePassword'); 
    } 

    return $success; 
} 

視圖(用戶/ fpre):

if ($msg){ 
    echo 'Message: '.$msg; 
} 

echo form_open(); 
echo form_input('email', $email); 
echo form_password('password'); 
echo form_password('newpassword'); 
echo form_password('newpassconf'); 
echo form_submit('', 'Enviar'); 
echo form_close(); 

的變化進行比較: enter image description hereenter image description here

+0

感謝您的幫助..它的工作...我們可以更新當前密碼輸出電子郵件和當前密碼? –

+0

是的,您需要使用CodeIgniter會話庫(https://www.codeigniter.com/user_guide/libraries/sessions.html)實現登錄到您的項目。 –

0

我相信這條線$this->db->update('users','password');應該是$this->db->update('users', $data);

現在你沒有將密碼傳遞給更新函數。您正在傳遞字符串「密碼」。

+0

沒有..我嘗試過,但仍然沒有改變表用戶。密碼保持舊。 –

+0

正如Parker1090所說的,當您調用updatePassword函數時,您也可以將參數向後移動。 – Jason

1

你的模型函數顯示的參數預計是電子郵件,然後是密碼,但你的控制器通過其他方式傳遞它們。

$this->user_model->updatePassword($data,$email); 

應該是:

$this->user_model->updatePassword($email,$data); 

我還認爲,需要對數據進行不同的傳遞。 where()函數需要where(field_name, value)where(array(field_name => value))。看着你的代碼,你似乎在混合這兩個。

使用set()應該在這方面幫助過,所以不是

$data1=array('password'=>$data); 
$this->db->where('email','$email'); 
$this->db->update('users','password'); 

用途:

$this->db->set($data); 
$this->db->where($email); 
$this->db->update('users'); 

注:未測試的代碼。

+0

*謝謝兄弟* –

相關問題