Nick0o7
Nick0o7

Reputation: 11

server side php email validation not working

I am practicing PHP and I created a signup form that performs client-side validation and also server-side. Javascript works perfectly and performs client-side but I removed all the javascript to see whether server-side works or not but it is not working and it submits the form without showing my errors.

Here is the Server-side code:

if($_POST)
{

    $query = "SELECT `email` FROM `users` where email =" . "'" . $_POST['Email'] . "'";

    $result = mysqli_query($db,$query);

    $row = mysqli_fetch_array($result);

    if($_POST['Email'] == $row['email'])
    {

        $error .= "This Email address is already registered with us";

    }

    if($error != "")
    {

        $error = '<div class="alert alert-danger" role="alert"><p>There were error(s) in your form:</p>' . $error . '</div>';

    }
}

Here is the form Code:

<form method="post">
                <div class="form-group">
                    <label for="Email">Email address</label>
                    <input type="email" class="form-control" id="Email" name="Email" aria-describedby="emailHelp">
                    <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
                </div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>

Upvotes: 0

Views: 80

Answers (1)

clash
clash

Reputation: 427

Executing a SELECT query will always result in a array, so $row is an array and the email has to be checked like this

if($_POST['Email'] == $row['email'])

Logging the variable $row will give you more information.

There are other ways to check if the query found something, look: https://www.php.net/manual/de/mysqli-result.num-rows.php

Upvotes: 1

Related Questions