0

How do I add a email validation to this pdo script. the user enters their email address in 'textfield'. simply if the email address entered is not in the database I want it to redirect to fail.html

<?php

$host=""; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name="orders"; // Table name  
$email = $_POST['textfield'];


        $db = new PDO('mysql:host='.$host.
                          ';dbname='.$db_name.
                          ';charset=UTF-8',
                    $username, $password);
        $stmt = $db->prepare('SELECT * FROM `orders` WHERE `email`=:email LIMIT 1');
        $stmt->bindValue(':email', $email, PDO::PARAM_STR);
        $stmt->execute();
        $result = $stmt->fetch(PDO::FETCH_ASSOC);

        echo "The is: ".$result['name'].", and mail is: ".$result['email']." . Status: ".$result['status'];


  ?>
7

1 Answer 1

2
if($stmt->rowCount()>0)
{
    echo "The result is: ".$result['name'].", and mail is: ".$result['email']." . Status: ".$result['status'];
}
else
{
    echo "Email not found in the database!";
}

Instead of echoing an error message, you can redirect to another page (only if nothing has already been sent to the browser):

header('Location: fail.html');

Or you can include the file to display it in the current page:

require 'fail.html';

Or you can use it in a form input field:

echo '<input name="login" type="text" value="' . $result['name'] . '>';

The field has its name attribute set to login, so you will be able to refer to it once the form is submitted.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks works. If i want to display using textfield by '<input type="text" value="<?php echo $result['name']; ?>" /> rather than echo "The result is: ".$result['name']....... how would I go about changing the validation?
not sure why but when i replace echo "Email not found in the database!"; with header('Location: fail.html'); it does not redirect if email is wrong
I wrote in my answer (only if nothing has already been sent to the browser). Read Headers already sent to learn how to fix the failing header redirect.
ok got it now. the textfield where I want to display the results is in the body of the page. Can I echo the results in the body rather than in the PHP script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.