943,616 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 526
  • PHP RSS
Jul 21st, 2009
0

sql error please help

Expand Post »
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.

PHP Syntax (Toggle Plain Text)
  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.  
Similar Threads
k2k
Reputation Points: 15
Solved Threads: 1
Posting Whiz
k2k is offline Offline
351 posts
since Nov 2007
Jul 21st, 2009
0

Re: sql error please help

is the code working already with the original query?
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007
Jul 21st, 2009
0

Re: sql error please help

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
k2k
Reputation Points: 15
Solved Threads: 1
Posting Whiz
k2k is offline Offline
351 posts
since Nov 2007
Jul 21st, 2009
0

Re: sql error please help

How about the tables or the fields?Are they correct?The code seems fine.
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007
Jul 21st, 2009
0

Re: sql error please help

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
k2k
Reputation Points: 15
Solved Threads: 1
Posting Whiz
k2k is offline Offline
351 posts
since Nov 2007
Jul 21st, 2009
1

Re: sql error please help

No problem bro, mark this thread as solved now. Don't forget the rep!
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007
Jul 21st, 2009
0

Re: sql error please help

No problem bro, mark this thread as solved now. Don't forget the rep!

of course, thanks again.
k2k
Reputation Points: 15
Solved Threads: 1
Posting Whiz
k2k is offline Offline
351 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: how to edit the uploaded file in php???
Next Thread in PHP Forum Timeline: import from excel to database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC