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'];
?>
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 echo
ing 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.