I’m trying to have a simple form with ‘email’ and ‘password’ to be posted into my DB. Here is my code so far:
if (array_key_exists('email',$_POST) || array_key_exists('password', $_POST)){
$link = mysqli_connect("localhost", "root", "", "users");
if (mysqli_connect_error()) {
die ("Connection has failed");
};
$query = "SELECT `email` FROM users WHERE email = '".mysqli_real_escape_string($link, $_POST['email'])."'";
if (mysqli_query($link, $query)) {
echo "<p>Email address is already registered!!</p>";
} else {
$query = "INSERT INTO `users` (`email`,`password`) VALUES ('".mysqli_real_escape_string($link, $_POST['email'])."', '".mysqli_real_escape_string($link, $_POST['password'])."')";
mysqli_query($link, $query);
echo "Congrats! You are now signed-up!";
};
};
I believe that there is something wring with this statement:
if (mysqli_query($link, $query)) {
echo "<p>Email address is already registered!!</p>"
Because no matter what my entry is, it always returns “Email address is already registered!!”, but I just do not understand why it does not go to the “else” statement if the query is “false” !!
Can someone please explain !?