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!