0

アクティブなレコードを使用してパスワード変更コードを作成しようとしています。それは問題なく動作しますが、パスワードが正常に変更されたかどうか、または現在のパスワードが実際に間違っていたかどうかを知る必要があります。

影響を受けた行を使用して、影響を受けたレコードの数を確認しています。値は 1 または 0 である必要があります。パスワードが正常に変更された場合は 1、変更されていない場合は 0 (つまり、入力された現在のパスワードが間違っていた) です。ユーザー名が一意であるため、影響を受ける行が 1 を超えることはありません。したがって、私がアプローチしている方法で機能するはずです。ただし、影響を受ける行関数は常に 1 を返すため、機能していないようです。

コード コントローラーは次のとおりです。

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

モデル:

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());
}
4

4 に答える 4

2

このようにモデルを変更します

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

「=」の代わりに「==」を追加して、コントローラーのifステートメントを変更します

if($flag == TRUE)// If Successful
{
    $this->session->set_flashdata('message', $flag);
}
于 2014-02-19T09:40:19.047 に答える
0

クエリ キャッシングが原因である可能性があります。この特定のクエリのクエリ キャッシングを切り替えて試してみてください。

// Turn caching off for this one query 
  $this->db->cache_off();
  $data = array('pass'=>$change);
  $this->db->where('id',$id);
  $this->db->where('pass',$current);
  $this->db->update('users',$data);
  return ($this->db->affected_rows());
于 2013-06-11T07:08:49.850 に答える
0
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);

  //comment the return
  //return ($this->db->affected_rows());

  // echo formatted last query
  echo '<pre>';
  echo $this->db->last_query();
  echo '</pre>';
  die();
 }

更新を行うための正しい変数を提供していますか? モデルでこれを試してからテストしてください。phpmyadminまたは、オンにprofilerして、実行されているクエリと渡されているパラメーターを確認します。

または、試すことができます

var_dump($this->db->affected_rows());

それが何を返すかを見るために。

于 2013-06-11T07:57:53.510 に答える
-2

このコードを試してください

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);
 $query = $this->db->affected_rows();
 if($query > 0){
    return true;
 }else{
    return false;
 }
}
于 2017-10-11T05:03:18.207 に答える