sql error please help

Thread Solved

Join Date: Nov 2007
Posts: 227
Reputation: k2k is an unknown quantity at this point 
Solved Threads: 0
k2k k2k is offline Offline
Posting Whiz in Training

sql error please help

 
0
  #1
Jul 21st, 2009
i just downloaded a login script and the code below is the php backend for the login. It is originally using md5 encryption and decryption for the password. For some reason I want to manage the password without encryption for now. my database has a member with login name and password. please see the line indicated with " ***" it keep complaining "databaseQuery failed"
I really have no more idea what could be wrong with that line of code, please help me. Thanks.

  1. <?php
  2. //Start session
  3. session_start();
  4.  
  5. //Include database connection details
  6. require_once('config.php');
  7.  
  8. //Array to store validation errors
  9. $errmsg_arr = array();
  10.  
  11. //Validation error flag
  12. $errflag = false;
  13.  
  14. //Connect to mysql server
  15. $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
  16. if(!$link) {
  17. die('Failed to connect to server: ' . mysql_error());
  18. }
  19. else{echo" connection is good";}
  20.  
  21. //Select database
  22. $db = mysql_select_db(DB_DATABASE);
  23. if(!$db) {
  24. die("Unable to select database");
  25. }
  26. else{echo "connected to database";}
  27.  
  28. //Function to sanitize values received from the form. Prevents SQL injection
  29. function clean($str) {
  30. $str = @trim($str);
  31. //get_magic_quotes_gpc returns 0 if off, 1 otherwise
  32. if(get_magic_quotes_gpc()) {
  33. $str = stripslashes($str);
  34. }
  35. return mysql_real_escape_string($str);
  36. }
  37.  
  38. //Sanitize the POST values
  39. $login = clean($_POST['login']);
  40. $password = clean($_POST['password']);
  41.  
  42. //Input Validations
  43. if($login == '') {
  44. $errmsg_arr[] = 'Login ID missing';
  45. $errflag = true;
  46. }
  47. if($password == '') {
  48. $errmsg_arr[] = 'Password missing';
  49. $errflag = true;
  50. }
  51.  
  52. //If there are input validations, redirect back to the login form
  53. if($errflag) {
  54. $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
  55. session_write_close();
  56. header("location: login-failed.php");
  57. exit();
  58. }
  59. *************** error below *****************
  60. //Create query
  61. $qry="SELECT * FROM members WHERE login='$login' AND passwd='$password' ";
  62. /*** this is the original but i don't need md5 so i modified it to be the above one*** /
  63. //$qry="SELECT * FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'";
  64.   ************ end error ************************
  65. $result=mysql_query($qry);
  66.  
  67. //Check whether the query was successful or not
  68. if($result) {
  69. if(mysql_num_rows($result) == 1) {
  70. //Login Successful
  71. session_regenerate_id();
  72. $member = mysql_fetch_assoc($result);
  73. $_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
  74. $_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
  75. $_SESSION['SESS_LAST_NAME'] = $member['lastname'];
  76. session_write_close();
  77. header("location: member-index.php");
  78. exit();
  79. }else {
  80. //Login failed
  81. header("location: login-failed.php");
  82. exit();
  83. }
  84. }else {
  85. die("Query failed");
  86. }
  87. ?>
  88.  
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 568
Reputation: ryan_vietnow is an unknown quantity at this point 
Solved Threads: 71
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: sql error please help

 
0
  #2
Jul 21st, 2009
is the code working already with the original query?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 227
Reputation: k2k is an unknown quantity at this point 
Solved Threads: 0
k2k k2k is offline Offline
Posting Whiz in Training

Re: sql error please help

 
0
  #3
Jul 21st, 2009
Originally Posted by ryan_vietnow View Post
is the code working already with the original query?

people commented that the codes are working. however in my case, it is not working. i tested it that it is connected to the database successfully. and that line of select statement looks right to me.

actually i think everything else is working (when i left both login and password blank it would send me to the login-fail.php page as supposed to) it looks like it is just that line of query got some issue.

thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 568
Reputation: ryan_vietnow is an unknown quantity at this point 
Solved Threads: 71
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: sql error please help

 
0
  #4
Jul 21st, 2009
How about the tables or the fields?Are they correct?The code seems fine.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 227
Reputation: k2k is an unknown quantity at this point 
Solved Threads: 0
k2k k2k is offline Offline
Posting Whiz in Training

Re: sql error please help

 
0
  #5
Jul 21st, 2009
Originally Posted by ryan_vietnow View Post
How about the tables or the fields?Are they correct?The code seems fine.

huh, thanks for your help. that was a good guess. i got 2 database and i connected to the wrong one. = P

thanks a ton
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 568
Reputation: ryan_vietnow is an unknown quantity at this point 
Solved Threads: 71
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: sql error please help

 
1
  #6
Jul 21st, 2009
No problem bro, mark this thread as solved now. Don't forget the rep!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 227
Reputation: k2k is an unknown quantity at this point 
Solved Threads: 0
k2k k2k is offline Offline
Posting Whiz in Training

Re: sql error please help

 
0
  #7
Jul 21st, 2009
Originally Posted by ryan_vietnow View Post
No problem bro, mark this thread as solved now. Don't forget the rep!

of course, thanks again.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC