Jump to content

Login page unsuccessful


LiquidFusi0n

Recommended Posts

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

Apologies for this 'self-bump' but this is getting beyond a joke  :wtf:

 

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 :P I really want to know WHY it isn't working  8)

 

--LiquidFusi0n

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.