Page 1 of 1

What Is Wrong With Number Of Rows Count Function ?

Posted: Sun Apr 09, 2017 6:55 pm
by UniqueIdeaMan
Guys & My Ladies,

This LOGIN.php was working lastnight and so I don't know why not now.

The ELSE at 53 gets triggered even though I have typed the right password!

What do you think of line 16 ? ...

if($numrows >1)

I tried the following but no luck:

if($numrows)

if($numrows !=0)

if($numrows ==2)



In the past, they worked, though. What is wrong, do you reckon ?


Code: Select all

<?php
session_start();
require "conn.php";
require "site_details.php";

if(isset($_POST["member_login_submit"]))
{
    if(!empty($_POST["member_login_username_or_email"]) && !empty($_POST["member_login_password"]))
    {
        $member_login_username_or_email = trim(strip_tags(strtolower(mysqli_real_escape_string($conn,$_POST["member_login_username_or_email"]))));
        $member_login_password = trim(strip_tags(mysqli_real_escape_string($conn,$_POST["member_login_password"])));
        
        $sql = "SELECT * FROM users WHERE usernames='".$member_login_username_or_email."' OR emails='".$member_login_username_or_email."' AND passwords='".$member_login_password."'";
        $result = mysqli_query($conn,$sql);
        $numrows = mysqli_num_rows($result);
        if($numrows >1)
        {        
            while ($row = mysqli_fetch_assoc($result))
            {
                $db_username = $row["usernames"];
                $db_password = $row["passwords"];
                $db_email = $row["emails"];
                                        
                if  ($member_login_username_or_email == $db_username && $member_login_password == $db_password || $member_login_username_or_email == $db_email && $member_login_password == $db_password)            
                {
                    $_SESSION["user"] = $member_login_username_or_email;           
                    if(!empty($_POST["member_login_remember"]))
                    {
                        setcookie("member_login_username_or_email", $member_login_username_or_email, time()+ (10 * 365 * 24 * 60 * 60));
                        setcookie("member_login_password", $member_login_password, time()+ (10 * 365 * 24 * 60 * 60));                        
                    }
                    else
                    {
                        if(isset($_COOKIE["member_login_username_or_email"]))
                        {    
                            setcookie("member_login_username_or_email", "", "");
                        }
                        if(isset($_COOKIE["member_login_password"]))
                        {    
                            setcookie("member_login_password", "", "");
                        }        
                    }
                    header("location:home.php");            
                }
                else
                {
                    $message = "Invalid login!";
                }    
            }
        }
        else
        {
            $message = "Something is wrong! Try again later!";
        }        
    }
    else
    {
        $message = "You must input your Username and Password!";    
    }
}    

?>
<!DOCTYPE html>
<html>
<head>
<title><?php $site_name?> Member Login Page</title>
  <meta charset="utf-8">
</head>
<body>
<div class = "container">
<form method="post" action="">
<center><h3><?php $site_name ?> Member Login Form</h3></center>
<div class="text-danger">
<?php
if(isset($message))
{
    echo $message;
}
?>
<div class="form-group">
<center><label>Username/Email:</label>
<input type="text" placeholder="Enter Username or Email" name="member_login_username_or_email" value="<?php if(isset($_COOKIE["member_login_username_or_email"])) echo $_COOKIE["member_login_username_or_email"]; ?>"</center>
</div>
<div class="form-group">
<center><label>Password:</label>
<input type="password" placeholder="Enter password" name="member_login_password" value="<?php if(isset($_COOKIE["member_login_password"])) echo $_COOKIE["member_login_password"]; ?>"></center>
</div>
<div class="form-group">
<center><label>Remember Login Details:</label>
<input type="checkbox" name="member_login_remember" /></center>
</div>
<div class="form-group">
<center><input type="submit" name="member_login_submit" value="Login" class="button button-success" /></center>
</div>
<div class="form-group">
<center><font color="red" size="3"><b>Forgot your password ?</b><br><a href="member_login_password_reset.php">Reset it here!</a></font></center>
<center><font color="red" size="3"><b>Not registered ?</b><br><a href="member_register.php">Register here!</a></font></center>
</form>
</div>
</body>
</html>

Re: What Is Wrong With Number Of Rows Count Function ?

Posted: Mon Apr 10, 2017 5:46 am
by Celauran
UniqueIdeaMan wrote:This LOGIN.php was working lastnight and so I don't know why not now.

The ELSE at 53 gets triggered even though I have typed the right password!

What do you think of line 16 ? ...

if($numrows >1)
Why > 1? Why are you expecting to have more than one login with the same credentials?

Re: What Is Wrong With Number Of Rows Count Function ?

Posted: Mon Apr 10, 2017 3:12 pm
by UniqueIdeaMan
Celauran wrote:
UniqueIdeaMan wrote:This LOGIN.php was working lastnight and so I don't know why not now.

The ELSE at 53 gets triggered even though I have typed the right password!

What do you think of line 16 ? ...

if($numrows >1)
Why > 1? Why are you expecting to have more than one login with the same credentials?
No.

Folks,

Have you seen on gmail or yahoomail or something you can either enter your username or email and then the password and it would log you in ? Trying to build like that so user gets a choice to either use his username or email to login. Script should log user in aslong as "either the username or email" is a match in the username\email column and the password is a match too that is relevant to the username/email.
So, db is like this:

Username|Email|Pass

If the username is a match on row position 5 then the password should be a match on row position 5 too.
Or,
If the email is a match on row position 5 then the password should be a match on row position 5 too.

I think you understand. Pretty basic, really. Nothing complicated.
Actually, I'm very curious to see how you would code it. A sample code is most appreciated.

Remember, looking for 1 user on each occassion. His username match and his password match. That makes 2 of his inputs matches I'm looking for. All the youtube tuts showed to code like this.

1st_user_username
1st_user_password

2nd_user_username
2nd_user_password

I tried logging in using the 1st_user_username and 2nd_user_password but it didn't log me in. Hence, I thought my code was correct. What is your conclusion now to my code ?

Re: What Is Wrong With Number Of Rows Count Function ?

Posted: Tue Apr 11, 2017 6:23 am
by Celauran
UniqueIdeaMan wrote:
Celauran wrote:
UniqueIdeaMan wrote:This LOGIN.php was working lastnight and so I don't know why not now.

The ELSE at 53 gets triggered even though I have typed the right password!

What do you think of line 16 ? ...

if($numrows >1)
Why > 1? Why are you expecting to have more than one login with the same credentials?
No.

Folks,

Have you seen on gmail or yahoomail or something you can either enter your username or email and then the password and it would log you in ? Trying to build like that so user gets a choice to either use his username or email to login. Script should log user in aslong as "either the username or email" is a match in the username\email column and the password is a match too that is relevant to the username/email.
So, db is like this:

Username|Email|Pass

If the username is a match on row position 5 then the password should be a match on row position 5 too.
Or,
If the email is a match on row position 5 then the password should be a match on row position 5 too.
The username and email would still need to be unique, so you're still getting either no rows or exactly one row.