gpt4 book ai didi

php - 检查有效电子邮件以及是否存在

转载 作者:行者123 更新时间:2023-11-29 12:49:47 24 4
gpt4 key购买 nike

我已经成功地允许用户通过向 friend 发送一封包含唯一邀请码的电子邮件来邀请他们,

  • 但是,我正在尝试添加检查它是否是有效电子邮件地址以及该电子邮件是否已在另一个表“用户”(同一数据库)中注册的功能,因为对于已经注册的人来说这将是一件令人头疼的事情接收邀请电子邮件。

我尝试通过编写以下脚本来检查有效的电子邮件以及它是否存在:

  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';
}

在注册用户时检查电子邮件地址是成功的,但注册帐户与已注册帐户位于同一个表中。我不确定如何在邀请代码中使用相同的脚本,因为我正在尝试检查注册的单独表格中的电子邮件。

目前它不检查它是否是有效的电子邮件或是否存在。

完整的 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
}
}
?>

我只是想检查电子邮件是否已在“用户”表中注册以及输入的电子邮件是否有效。

最佳答案

我认为您的主要问题是如何检查另一个表中是否存在电子邮件。如果这是错误的,请告诉我,我可以更新我的答案:P 这是您应该能够使用的函数的草稿。

我假设您有这两个表:

表 1:用户

||id||email||name||

表 2:邀请

||id||email||inviter_user_id||

您可以使用此函数检查电子邮件是否存在于任一表中

<?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;
}
}
?>

关于php - 检查有效电子邮件以及是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24937169/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com