2017-02-17 2 views
2

model.php업데이트 쿼리가 codeigniter에서 작동하지 않습니까?

public function verifyLogin($username, $entered_password, $captcha_input) { 

    $this->db->trans_start(); 

    $sql = "SELECT 
       name, 
       selector, 
       password, 
       login_attempts 
      FROM 
       company 
      WHERE 
       username = ? 
      LIMIT 
       1 
      "; 

    $query = $this->db->query($sql, $username); 
    $result_num = $query->num_rows(); 

    if ($result_num == 1) { 

     $stored_password = $query->row()->password; 
     $login_attempts = $query->row()->login_attempts; 

     if (crypt($entered_password, $stored_password) == $stored_password) { 

      if ($captcha_input == $this->session->userdata('captchaWord')) 
      { 
       //recreate captcha 
      } 
      $data['company_name']  = $query->row()->name; 
      $data['company_selector'] = $query->row()->selector; 

      //Not working 
      $sql = "UPDATE 
         company 
        SET 
         login_attempts = 0 
        WHERE 
         username = ? 
        "; 
      $query = $this->db->query($sql, $username); 
      unset($this->captcha); 

      return $data; 
     } 
     else { 

      if ($login_attempts >= 3) 
      { 
       //Enable CAPTCHA 
       $random_number = substr(number_format(time() * rand(),0,'',''),0,6); 
       $vals = array(
       'word'   => $random_number, 
       'img_path'  => './captcha/', 
       'img_url'  => base_url().'captcha/', 
       'font_path'  => './path/to/fonts/texb.ttf', 
       'img_width'  => '150', 
       'img_height' => 30, 
       'expiration' => 7200, 
       'word_length' => 8, 
       'font_size'  => 16, 
       'img_id'  => 'Imageid', 
       'pool'   => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 

       // White background and border, black text and red grid 
       'colors'  => array(
         'background' => array(255, 255, 255), 
         'border' => array(255, 255, 255), 
         'text' => array(0, 0, 0), 
         'grid' => array(255, 40, 40) 
        ) 
       ); 

       $this->captcha = create_captcha($vals); 
       $this->session->set_userdata('captchaWord', $random_number); 
      } 

      //Not working 
      $sql = "UPDATE 
         company 
        SET 
         login_attempts = login_attempts + 1 
        WHERE 
         username = ? 
        "; 
      $query = $this->db->query($sql, $username); 

      echo $this->db->last_query(); 
      return FALSE; 
     } 
    } else { 
     return 0; 
    } 

    $this->db->trans_complete(); 

    if ($this->db->trans_status() === FALSE) { 
     return 'Transaction failed'; 
    } 
} 

controller.php

public function testAdminLogin() { 
    $this->load->model('Company_model'); 

    $is_logged_in = $this->Company_model->verifyLogin('sarah', '33123', ''); 

    if ($is_logged_in === 0) { 

     echo "No username found"; 
    } 
    else if ($is_logged_in === FALSE) { 

     echo "Password not matched"; 
     $data['cap'] = $this->Company_model->getCaptcha(); 

     if (isset($data['cap'])) 
     { 
      $this->load->view('test_captcha', $data); 
     } 
    } 
    else if ($is_logged_in === 'Transaction failed') { 
     //possibly an error 
    } 
    else { 
     //successful 

     $session_array = array(
      'company_selector' => $is_logged_in['company_selector'], 
      'company_name' => $is_logged_in['company_name'] 
      ); 

     $this->session->set_userdata('session_array', $session_array); 
     echo "Login successful. Company selector: ".$this->session->userdata['session_array']['company_selector']; 
    } 
} 

나는 데이터베이스에서이 레코드가 업데이트되지 않습니다 실행합니다. 1 씩 증가하지만 값은 동일하게 유지됩니다. 에코 된 SQL 문이 $this->db->last_query()에서 올 바릅니다. MySQL 워크 벤치에 직접 복사하고 붙여 넣으면 변경 사항이 적용되었습니다.

액티브 레코드를 사용하지 않으려 고 시도했습니다.

답변

0

$this->db->trans_complete();과 관련이 있습니다. 사용자가 잘못된 암호를 입력하면 즉시 값을 반환하므로 트랜잭션이 완료되지 않고 변경 내용이 커밋되지 않습니다.

잘못된 암호를 확인하기 위해 대신 플래그를 설정해야했습니다. $this->db->trans_complete(); 후 다음

private $wrong_password = FALSE; 

if ($login_attempts >= 3) 
       { 
        //Enable CAPTCHA 
        $random_number = substr(number_format(time() * rand(),0,'',''),0,6); 
        $vals = array(
        'word'   => $random_number, 
        'img_path'  => './captcha/', 
        'img_url'  => base_url().'captcha/', 
        'font_path'  => './path/to/fonts/texb.ttf', 
        'img_width'  => '150', 
        'img_height' => 30, 
        'expiration' => 7200, 
        'word_length' => 8, 
        'font_size'  => 16, 
        'img_id'  => 'Imageid', 
        'pool'   => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 

        // White background and border, black text and red grid 
        'colors'  => array(
          'background' => array(255, 255, 255), 
          'border' => array(255, 255, 255), 
          'text' => array(0, 0, 0), 
          'grid' => array(255, 40, 40) 
         ) 
        ); 

        $this->captcha = create_captcha($vals); 
        $this->session->set_userdata('captchaWord', $random_number); 
       } 

       // $data = array(
       //  'login_attempts' => 'login_attempts + 1' 
       // ); 
       // $this->db->where('username', $username); 
       // $this->db->update('company', $data); 
       $sql = "UPDATE 
          company 
         SET 
          login_attempts = login_attempts + 1 
         WHERE 
          username = ? 
         "; 
       $query = $this->db->query($sql, $username); 

       $this->wrong_password = TRUE; 

:

if ($this->db->trans_status() === FALSE) { 
     return 'Transaction failed'; 
    } else { 

     if ($this->wrong_password === TRUE) { 
      return FALSE; 
     } 
    } 

이러한 정직한 실수.