问题描述
通过向用户发送包含唯一邀请码的电子邮件,我已经成功地允许用户邀请朋友
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.
完整的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.
推荐答案
我认为您的主要问题是如何检查另一个表中是否存在电子邮件.如果那是错误的,请告诉我,我可以更新我的答案:P这是您应该可以使用的功能的粗略草案.
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:
表1:用户
||id||email||name||
表2:邀请
||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;
}
}
?>
这篇关于检查有效的电子邮件以及是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!