LiquidFusi0n Posted September 25, 2011 Share Posted September 25, 2011 I always feel bad for my first post on a new forum being a question. But this has me stuck I am writing a very simple log-in script. To check I am using 'mysql_num_rows' and seeing that only one row is returned. I have tried debugging this, and using Google but to no avail. The page is saying 'Login unsuccessful' even when the values are correct. Any help is greatly appreciated. <html> <head> <title> Log-in </title> </head> <body> <form action="login.php" method="POST"> Username: <input type="text" name=username"> Password: <input type="password" name="password"> <input type="submit" name="login" value="Login"> </form> <?php if(isset($_POST['username'])){ $un = $_POST['username']; } if(isset($_POST['password'])){ $pass = $_POST['password']; } $db = mysql_connect("127.0.0.1:3306", "root" , "password"); //Not actual password btw if(!$db) { die("Error connecting to database: " . mysql_error()); } $db_used = mysql_select_db("login", $db); if(!$db_used){ die("Could not select database: " . mysql_error()); } $user_name = mysql_real_escape_string($un); $password = mysql_real_escape_string($pass); $query = "SELECT * FROM details WHERE un=" . $user_name . " AND pwd=" . $password. ""; $query2 = "SELECT * FROM details WHERE un='$user_name' AND pwd='$password'"; //This was for debugging, seeing if it was the passing that was failing $result = mysql_query($query); if(!$result){ echo "There was an error processing your request: " . mysql_error() . ""; } $check = mysql_num_rows($result); if($check == 1){ echo "Login successful, welcome back " . $user_name . ""; } else{ echo "Login unsuccessful, please ensure you are using the correct details"; } ?> </body> </html> Thanks in advance guys.... --LiquidFusi0n Quote Link to comment https://forums.phpfreaks.com/topic/247844-login-page-unsuccessful/ Share on other sites More sharing options...
WebStyles Posted September 25, 2011 Share Posted September 25, 2011 here's a simplified version that should work: (not the best way to do it though...) <html> <head> <title> Log-in </title> </head> <body> <?php if($_SERVER['REQUEST_METHOD']=="POST"){ $db = mysql_connect("127.0.0.1:3306", "root" , "password")or die("Error connecting to database: " . mysql_error()); $db_used = mysql_select_db("login", $db)or die("Could not select database: " . mysql_error()); $user_name = mysql_real_escape_string($_POST['username'],$db); $password = mysql_real_escape_string($_POST['password'],$db); $query = mysql_query("SELECT * FROM `details` WHERE `un` = '$user_name' AND `pwd` = '$password'",$db) or die(mysql_error()); if(mysql_num_rows($query) == 1){ echo "Login successful, welcome back " . $user_name . ""; }else{ echo "Login unsuccessful, please ensure you are using the correct details"; } }else{ ?> <form action="login.php" method="POST"> Username: <input type="text" name=username"> Password: <input type="password" name="password"> <input type="submit" name="login" value="Login"> </form> <?php } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/247844-login-page-unsuccessful/#findComment-1272662 Share on other sites More sharing options...
LiquidFusi0n Posted September 25, 2011 Author Share Posted September 25, 2011 Thank you, I will use that if I can't get my own code fixed I am still failing to see why my code is not working... the syntax/logic looks sound to me? --LiquidFusi0n Quote Link to comment https://forums.phpfreaks.com/topic/247844-login-page-unsuccessful/#findComment-1272664 Share on other sites More sharing options...
LiquidFusi0n Posted September 26, 2011 Author Share Posted September 26, 2011 Apologies for this 'self-bump' but this is getting beyond a joke I have now added error checking at every stage and no errors are given. This to means it MUST be an error with 'mysql_num_rows'. Anyways I have re-done the code for you guys and I will post the database structure so you can see for yourselves what is looks like MySQL Query With Result mysql> SELECT * FROM details WHERE un='test' AND pwd='pass'; +----+------+------+ | id | un | pwd | +----+------+------+ | 2 | test | pass | +----+------+------+ 1 row in set (0.00 sec) Code: <html> <head> <title> Log-in </title> </head> <body> <form action="login.php" method="POST"> Username: <input type="text" name=username"> Password: <input type="password" name="password"> <input type="submit" name="login" value="Login"> </form> <?php $un = ''; if(isset($_POST['username'])){ $un = $_POST['username']; } $pass = ''; if(isset($_POST['password'])){ $pass = $_POST['password']; } $db = mysql_connect("127.0.0.1:3306", "root" , "password"); //Not actual password btw if(!$db) { die("Error connecting to database: " . mysql_error()); } $db_used = mysql_select_db("login", $db); if(!$db_used){ die("Could not select database: " . mysql_error()); } $user_name = mysql_real_escape_string($un); $password = mysql_real_escape_string($pass); $query = "SELECT * FROM details WHERE un='$user_name' AND pwd='$password'"; //Exact same command works when given the MySQL directly $result = mysql_query($query); if(!$result){ die("SQL query failed: " . mysql_error()); //Query is not returning an error } if(mysql_num_rows($result) == 1) { echo "Login successful welcome back, " . $user_name . ""; } else{ echo "Login unsuccessful"; } ?> </body> </html> This is now past how to do it I really want to know WHY it isn't working --LiquidFusi0n Quote Link to comment https://forums.phpfreaks.com/topic/247844-login-page-unsuccessful/#findComment-1272754 Share on other sites More sharing options...
LiquidFusi0n Posted September 26, 2011 Author Share Posted September 26, 2011 OK Thread can be closed. After some more de-bugging it was clear that username wasn't being passed. Want to see the culprit? Username: <input type="text" name=username"> Typo's will be the end of us --LiquidFusi0n Quote Link to comment https://forums.phpfreaks.com/topic/247844-login-page-unsuccessful/#findComment-1272767 Share on other sites More sharing options...
Pikachu2000 Posted September 26, 2011 Share Posted September 26, 2011 Echo, echo, echo and echo some more when debgging! Quote Link to comment https://forums.phpfreaks.com/topic/247844-login-page-unsuccessful/#findComment-1272771 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.