Checking Login Availability
Currently, our registration has one problem - a new user of our site can register with an already existing login, which, of course, is unacceptable.
To solve the problem, it is necessary before the request
to add a new user to the database,
execute a SELECT query that
will check whether the desired login is taken or not. If
not taken - register, if taken - do not
register, but display a message about it.
Let's write this code:
<?php
if (!empty($_POST['login']) and !empty($_POST['password'])) {
$login = $_POST['login'];
$password = $_POST['password'];
$query = "SELECT * FROM users WHERE login='$login'";
$user = mysqli_fetch_assoc(mysqli_query($link, $query));
if (empty($user)) {
$query = "INSERT INTO users SET login='$login', password='$password'";
mysqli_query($link, $query);
$_SESSION['auth'] = true;
} else {
// login is taken, display a message about it
}
}
?>
Modify your code so that when attempting registration, a check for login availability is performed and, if it is taken, - display a message about it and ask to enter another login.