Checking for valid email and if it exists
I have successfully been able to allow users to invite a friend by sending them an email with a unique invite code,
I have tried to check for a valid email and if it exists by writing this script:
function email_registered($email) {
$email = sanitize($email);
return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `email` = '$email'"), 0) ==1) ? true : false;
}
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
$errors[] = 'A valid email address is required';
}
if (email_registered($_POST['email']) === true) {
$errors[] = 'Sorry, the email '' . $_POST['email'] . '' is already in use';
}
This was successful for checking emails addresses when registering a user but registering a account was in the same table as already registered account. I am unsure how to use the same script in the invite code as I am trying to check an email in the registered separate table.
Currently it does not check if it is a valid email or if it exists.
The Full PHP:
include 'config.php';
function email_registered($email) {
$email = sanitize($email);
return (mysqli_result(mysqli_query("SELECT mysqli_num_rows()(`user_id`) FROM `users` WHERE `email` = '$email'"), 0) ==1) ? true : false;
}
if(!empty($_POST)){
if(!empty($_POST['email'])){
$email = mysqli_real_escape_string($conn,$_POST['email']);
$length = 10;
$inviteCode = "";
$characters = "0123456789abcdefghijklmnopqrstuvwxyz";
for ($p = 0; $p < $length; $p++) {
$inviteCode .= $characters[mt_rand(10, strlen($characters))];
}
function email_registered($email)
{
if (!empty($email)) {
$ret_val = false;
$query = sprintf(
"SELECT id FROM users WHERE email = '%s'",
mysqli_real_escape_string($email)
);
$result = mysqli_query($query);
$num_rows = mysqli_num_rows($result);
if ($num_rows > 0) {
//email exists
?>
<p>User Exists</p>
<?php
$ret_val = true;
} else {
$query = sprintf(
"SELECT id FROM invites WHERE email = '%s'",
mysqli_real_escape_string($email)
);
$result = mysqli_query($query);
$num_rows = mysqli_num_rows($result);
if ($num_rows > 0) {
//email exists
?>
<p>User Exists</p>
<?php
$ret_val = true;
}
}
return $ret_val;
}
}
else {
$query = mysqli_query($conn, "INSERT INTO `referrals` (`email`, `inviteCode`) VALUES ('$email', '$inviteCode') "); }
//you might want to consider checking more here such as $query == true as it can return other statuses that you may not want
if($query){
include 'userinvite.php';
?>
<p> "Thank you for inviting your friends!"</p>
<?php
}
else{
?>
<p>Sorry there must have been a problem</p>
<?php
die('Error querying database. ' . mysqli_error($conn));
}
}
else {
?>
<p>Please enter an email</p>
<?php
}
}
?>
I am just trying to check if the email is registered in the 'users' table and if the email entered is a valid email.
I think your main question is how to check if an email exists in another table. If that's wrong, let me know and I can update my answer :P Here's a rough draft of a function you should be able to use.
I'm assuming you have these two tables:
Table 1: users
||id||email||name||
Table 2: invites
||id||email||inviter_user_id||
You can use this function to check if the email exists in either table
<?php
/**
* Check if the given email already exists in the DB
*
* @param $email string the email to check
*/
function email_exists($email)
{
if (!empty($email)) {
$ret_val = false;
$query = sprintf(
"SELECT id FROM users WHERE email = '%s'",
mysqli_real_escape_string($email)
);
$result = mysqli_query($query);
$num_rows = mysqli_num_rows($result);
if ($num_rows > 0) {
//email exists
$ret_val = true;
} else {
$query = sprintf(
"SELECT id FROM invites WHERE email = '%s'",
mysqli_real_escape_string($email)
);
$result = mysqli_query($query);
$num_rows = mysqli_num_rows($result);
if ($num_rows > 0) {
//email exists
$ret_val = true;
}
}
return $ret_val;
}
}
?>
链接地址: http://www.djcxy.com/p/92814.html
上一篇: 全球邮件验证
下一篇: 检查有效的电子邮件,如果它存在