Guest User

Untitled

a guest
Jul 17th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. I am using CodeIgniter Framework 2.02
  2.  
  3. A Database Error Occurred
  4. Error Number: 1054
  5. Unknown column 'anil' in 'where clause'
  6. SELECT user_id FROM users WHERE username = anil AND password = password
  7. Filename: D:xampphtdocsc_loginsystemdatabaseDB_driver.php
  8. Line Number: 330<
  9.  
  10. function check_login($username, $password)
  11. {
  12. $sha1_password = sha1($password);
  13.  
  14. // The Guy uses ' ? ' in the video for the below statement and it works
  15. // I tried replacing ? with $username & $password, but it didnt work..
  16. // When I use the ?, I think it does query the DB, but I get the error
  17. // Incorrect Username or Password, Even though it is correct.
  18. // I only have 1 record on my DB, db = c_login, table=users
  19. // Fields are user_id username password email name
  20.  
  21. $query_str = "SELECT user_id FROM users WHERE username = ? AND password = ?";
  22. $result = $this->db->query($query_str, $username, $sha1_password);
  23.  
  24. if($result->num_rows() == 1)
  25. {
  26. return $result->row[0]->user_id;
  27. }
  28. else
  29. {
  30. return false;
  31.  
  32. }
  33. }
  34.  
  35. public function login()
  36. {
  37. $this->form_validation->set_rules('username', 'Username', 'required|trim|max_length[50]|xss_clean');
  38. $this->form_validation->set_rules('password', 'Password', 'required|trim|max_length[50]|xss_clean');
  39.  
  40. // If form_validation has NOT been run, load the view login form
  41. if($this->form_validation->run() == FALSE)
  42. {
  43. $this->load->view('view_login');
  44. }
  45. else // Else process login
  46. {
  47. // Process input and login
  48. $username = $this->input->post('username');
  49. $password = $this->input->post('password');
  50.  
  51. $user_id = $this->User_model->check_login($username, $password);
  52.  
  53. if( ! $user_id)
  54. {
  55. // Login Failed Error
  56. $this->session->set_flashdata('login_error', TRUE);
  57. redirect('user/login');
  58. }
  59. else
  60. {
  61. // Log them in
  62.  
  63. $this->session->set_userdata(array(
  64. 'logged_in' => TRUE,
  65. 'user_id' => $user_id
  66. ));
  67.  
  68. redirect('user/main_page');
  69. }
  70.  
  71. }
  72.  
  73. }
  74.  
  75. function check_login($username, $password)
  76. {
  77. $sha1_password = sha1($password);
  78.  
  79. $this->db->select('user_id');
  80. $this->db->where('username', $username);
  81. $this->db->where('password', $sha1_password);
  82. $result = $this->db->get('users');
  83.  
  84. if($result->num_rows() == 1)
  85. {
  86. $row = $result->row();
  87. return $row->user_id;
  88. }
  89. else
  90. {
  91. return false;
  92. }
  93. }
  94.  
  95. if($user_id == FALSE)
  96. {
  97. $this->session->set_flashdata('login_error', TRUE);
  98. redirect('user/login', 'refresh');
  99. }
  100. else
  101. {
  102. $this->session->set_userdata(array(
  103. 'logged_in' => TRUE,
  104. 'user_id' => $user_id
  105. ));
  106.  
  107. redirect('user/main_page', 'refresh');
  108. }
  109.  
  110. SELECT user_id FROM users WHERE username = anil AND password = password
  111.  
  112. SELECT user_id FROM users WHERE username = 'anil' AND password = 'password'
  113.  
  114. $result = $this->db->query($query_str, array($username, $sha1_password));
Add Comment
Please, Sign In to add comment