0

I have been recently making a project - an online quiz, so I decided to make a registration/login system as well. I have faced some issues with the login part, so I decided to do everything from scratch, but the problem is still there.

The registration works fine, I managed to register 10 users without problems, but when I try to login it is always showing - Wrong login or password! though everything is correctly input and there are no duplicate records in my DB.

Here is the connection to the database:

<?php
$hostname = "localhost";
$username = "user";
$pass = "pass";
$db = "testdb";

$connect = mysqli_connect($hostname, $username, $pass) or die("Something went wrong!");

mysqli_select_db($connect, $db) or die("Couldn`t connect to database!");
?>`

login.php

<html>
<head>
<?php
require ('db.php');

if(isset($_POST['login']))
{
    $login = mysqli_real_escape_string($connect, $_POST['login']);
    $password = mysqli_real_escape_string($connect, $_POST['password']);

    if($login == NULL || $password == NULL)
    {
        echo "All fields must be completed!";
        exit();
    }

    else
    {
        $sql = "SELECT * FROM `users` WHERE `login` = '".$login."' AND `password` = '".$password."'";
        $result = mysqli_query($connect, $sql);

        if($sql->num_rows > 0)
        {
            echo "Successfully logged in!";
        }

        else
        {
            echo "Wrong login or password!";
        }
    }
}
?>
</head>

<body>
<div align="center">
<h1>Login</h1>
</div>

<form action="login.php" method="post" align="center">
<input name="login" type="text" placeholder="Login"><br><br>
<input name="password" type="password" placeholder="Password"><br><br>

<input name="login" type="submit" value="Login">
</form>

</body>
</html>

I am so desperate, I have been searching for a solution for days! I really do not know why is this happening. I would appreciate any help!

2 Answers 2

2

Your submit button has the same name as your username field.

<input name="login" type="submit" value="Login">

change it to something else.

Eg:

<input name="loginBtn" type="submit" value="Login">

And also change

if($sql->num_rows > 0)

to

if($result->num_rows > 0)
Sign up to request clarification or add additional context in comments.

3 Comments

It works!!! Thank you very very much! Omg! I will NEVER give same name to buttons and fields :)
@Max775 No probs, do make sure to vote for the best answer and all the best to you :)
@DanyalSandeelo Hey, sorry I didn't give you credit for noticing the flaw in $sql->num_rows, but your answer was count($result) if I remembered correctly. Before you edited your answer :)
2

$sql is string, you need to perform the check on resultSet

Change

 if($sql->num_rows > 0)

to

 if($result->num_rows > 0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.