ankur0101 Posted May 2, 2012 Share Posted May 2, 2012 Hi, I am making a login page which consists of 2 pages, index.php and config.php Following is a code of config.php <?php define("HOST", "localhost"); define("USER", "aaaabbbccc"); define("PASS", "aaabbbccc"); define("DATABASE", "pricetagindia"); $connection = @mysql_connect(HOST, USER, PASS); $select_db = @mysql_select_db(DATABASE, $connection); if(!$connection) { echo "<h1>Cannot connect to database</h1>"; exit(0); } if(!$select_db) { echo "<h1>Cannot select database</h1>"; exit(1); } ?> Here is a coding of index.php <?php include('config.php'); $_error = "<p></p>"; if (isset($_POST['username']) || isset($_POST['password'])) { $username = $_POST['username']; $password = $_POST['password']; if($username == NULL || $password == NULL) { $_error = "<p>Username and Password, both fields are required.</p>"; } else { // Check in DB $password = md5($password); $checkuser_query = "SELECT * FROM users WHERE username = '$username.' AND password = '$password'"; $checkuser_result = mysql_query($checkuser_query); $checkuser_count = mysql_num_rows($checkuser_result); if($checkuser_count == 1) { echo "match found"; } else { echo "nothing found error, username was ".$username." and pass is ".$password; echo "<p></p>"; echo $checkuser_result; } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Price Tag India</title> </head> <body> <center> <h1>Price Tag India</h1> <p> </p> </center> <center> <form action="index.php" method="post" target=""> <table width="389" height="168" border="0"> <tr> <td width="141">Username :</td> <td colspan="2"><label for="username"></label> <input type="text" name="username" id="username" /></td> </tr> <tr> <td>Password :</td> <td colspan="2"><label for="password"></label> <input type="password" name="password" id="password" /></td> </tr> <tr> <td> </td> <td width="56"><input type="submit" name="button" id="button" value="Submit" /></td> <td width="223"><input type="reset" name="button2" id="button2" value="Reset" /></td> </tr> <tr> <td> </td> <td colspan="2"> <?php echo $_error; ?> </td> </tr> </table> <p> </p> </form> </center> </body> </html> I have attached the screenshot of database. Problem is when I do valid login i.e. with user admin and its password, it is not working, I spent 1 hour for solving this issue but didnt find any solution. Its frustrating now.....I know its simple but still its not working. It is giving me error as nothing found error, username was admin and pass is 62cc2d8b4bf2d8728120d052163a77df Resource id #5 Quote Link to comment https://forums.phpfreaks.com/topic/261939-unable-to-do-select-function-its-strange/ Share on other sites More sharing options...
codebyren Posted May 2, 2012 Share Posted May 2, 2012 Check your SQL for fetching the user. You have a period appended to the username: $checkuser_query = "SELECT * FROM users WHERE username = '$username.' AND password = '$password'"; So if you enter "admin" as the username, your query is trying to find "admin." (i.e. with a period at the end). Quote Link to comment https://forums.phpfreaks.com/topic/261939-unable-to-do-select-function-its-strange/#findComment-1342221 Share on other sites More sharing options...
darkfreaks Posted May 2, 2012 Share Posted May 2, 2012 the appendage is not needed you can echo out username and password within double quotes. also to make the code mroe functioning you could change == to === also you have an extra dot like the above user stated in your SQL Quote Link to comment https://forums.phpfreaks.com/topic/261939-unable-to-do-select-function-its-strange/#findComment-1342222 Share on other sites More sharing options...
ankur0101 Posted May 2, 2012 Author Share Posted May 2, 2012 OMG, Its working, special thanks to user behicthebuilder :-) Also, can you tell me how can I make it more secure ? I mean how can I prevent sql injection ? Quote Link to comment https://forums.phpfreaks.com/topic/261939-unable-to-do-select-function-its-strange/#findComment-1342223 Share on other sites More sharing options...
darkfreaks Posted May 2, 2012 Share Posted May 2, 2012 if your using basic prevention check out strip_tags and mysql_real_escape_string but i would recommend using PDO Quote Link to comment https://forums.phpfreaks.com/topic/261939-unable-to-do-select-function-its-strange/#findComment-1342224 Share on other sites More sharing options...
ankur0101 Posted May 2, 2012 Author Share Posted May 2, 2012 That is why I love phpfreaks, problem solved in 15 minutes Quote Link to comment https://forums.phpfreaks.com/topic/261939-unable-to-do-select-function-its-strange/#findComment-1342225 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.