I have created this code in order to register users in my database. What I cannot manage to do, is to prevent adding the same user again and again. Here is my code:
connectDB();
$safe_fullname = mysqli_real_escape_string($mysqli,$_POST['fullname']);
$safe_email = mysqli_real_escape_string($mysqli,$_POST['email']);
$safe_password = mysqli_real_escape_string($mysqli,$_POST['pass']);
$addStatement="Insert into Users (Fullname,Email,Password,Is_Admin) values ('".$safe_fullname."','".$safe_email."','".$safe_password."','N')";
$result = mysqli_query($mysqli,$addStatement) or die(mysqli_error($mysqli));
Add unique to you table for (if it is meant to be unique) email or Username column:
ALTER TABLE Users ADD UNIQUE (Email);
OR
ALTER TABLE Users ADD UNIQUE (Username);
This way database only accepts one record with same email address or Username.
Other way to do this, is to select values from DB with given details. For example, let's use Email column:
$res = mysqli_query($mysqli, "SELECT Email FROM Users WHERE Email = '{$_POST['email']}'");
if(count($res) > 0) {
//exists => do stuff
} else {
//doesn't exist => do stuff
}