I am trying to figure out how to check if an email is already within a database as I don't want duplicates I have tried googling and it hasn't worked as old posts I think?
here's my code:
$emailcheck = $value2;
$emailcheck = "SELECT email FROM demo WHERE email= '$emailcheck' ";
$result = mysql_query($link, $emailcheck);
$count = mysql_num_rows($result);
if ($count > 0){
{$_SESSION['email'] = "Email is already used!";}
}
if (empty($_POST["name"]))
{$_SESSION['name']= "Name is required";}
if (empty($_POST["email"]))
{$_SESSION['email'] = "Email is required";} else {
if (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL))
{$_SESSION['email'] = "Email is incorect";}}
if(empty($_POST["name"]) || empty($_POST["email"]) || (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) || ($count > 0))
{
header("Location: home.php");
} else {
thank you for anyones help!
My nearly full code:
$value = $_POST['name'];
$value2 = $_POST['email'];
$emailcheck = $value2;
$emailcheck = "SELECT email FROM demo WHERE email= '$emailcheck' ";
$result = mysql_query($link, $emailcheck);
$count = mysql_num_rows($result);
if ($count > 0){
{$_SESSION['email'] = "Email is already used!";}
}
if (empty($_POST["name"]))
{$_SESSION['name']= "Name is required";}
if (empty($_POST["email"]))
{$_SESSION['email'] = "Email is required";} else {
if (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL))
{$_SESSION['email'] = "Email is incorect";}}
if(empty($_POST["name"]) || empty($_POST["email"]) || (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) || ($count > 0))
{
header("Location: home.php");
} else {
if (!$link) {
die('Could not connect: ' . mysql_errno());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('can\'t use ' . DB_NAME . ':' . mysql_error());
}
$sql = "INSERT INTO demo (name, email) VALUES ('$value', '$value2')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
header("location: https://www.google.co.uk/search?q=Ghostbusters&stick=H4sIAAAAAAAAAONgFuLUz9U3MI43skxR4gIxjUxLcrLTtYQck0vyi0LyQ8Ic81J888syU4sBdwGq3i0AAAA&sa=X&ved=0ahUKEwj0-J6QtuXaAhUQSsAKHaYqB2wQxA0I6gEwHg&biw=1280&bih=699");
mysql_close();
session_destroy();
}
}
?>