I have coded this in PHP and it works fine, it inserts my data from registration page to my database. It creates the account. I have tried to add a condition to check if the username already exists in the database, and if it does, i want to redirect him on to a new page saying that the username already exists. And if the username doesn't exist in the database, i want him to register normally. In my case, even if the username exists in the database it creates another one over the existing one. I have watched videos and read on the internet but i really can't understand how to do it. Sorry for my bad english and if i wasn't clear enough.
This is my code:
<?php
session_start();
// Create connection
include('config.php');
if (isset($_POST['buton_inregistrare'])) {
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
$password2 = mysqli_real_escape_string($conn, $_POST['password2']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$stergere = mysqli_real_escape_string($conn, $_POST['stergere']);
$realName = mysqli_real_escape_string($conn, $_POST['numeReal']);
$security = mysqli_real_escape_string($conn, $_POST['seqcod']);
$verUsername = mysqli_query($conn, "SELECT login FROM account WHERE name='$username'");
$count = mysqli_num_rows($verUsername);
if ($count > 0) {
header("location: ../inregistrare_esec2.html");
}
//check if passwords are the same
if ($password == $password2) {
//Creating user
$sql = "INSERT INTO account(login, password, email, social_id, real_name, securitycode) VALUES('$username', PASSWORD('$password'), '$email', '$stergere', '$realName', '$security')";
mysqli_query($conn, $sql);
header("location: ../inregistrare_succes.html");
}
else{
// passwords are not the same
header("location: ../inregistrare_esec.html");
}
}
?>
And this is how my mysql looks like: mysql image