我已经搜索了这个网站和互联网,寻找一个很好的简单解决方案,如何检查电子邮件是否被输入两次到我的数据库中,以及它是否是有效的电子邮件。我一直无法找到一个简单的解决方案。这是我的表单和 php。提前致谢
<div class="email-survey">
<?php if(isset($success)) { ?>
<div class="success_survey">Thanks you! Your survey has been submitted</div>
<?php } ?>
<?php if(isset($error)) { ?>
<div class="error_survey">
<?php echo $error; ?>
</div>
<?php } ?>
<form name="settings" action="/survey-confirm.php" method="post">
<input type="text" name="email" /> <br />
<input type="submit" name="submit" value="submit" />
</form>
</div>
require_once($_SERVER['DOCUMENT_ROOT'] . '/includes/system/init.php');
if(isset($_POST['submit'])) {
$email = $_POST['email'];
if(empty($_POST['email'])) {
$error = "Please enter a valid email";
}else{
$success = true;
mysql_query("INSERT INTO survey_email
(email) VALUES('".$_POST['email']."' ) ")
or die(mysql_error());
header('Location: /survey-email-confirm.php');
}
}
?>
向用户发送链接。当他们单击它时,请使用$_GET
信息来验证它是有效的电子邮件。当然,您应该使用 MySQL SELECT
查询来测试电子邮件是否存在以防止重复条目。下面显示了mysqli
面向对象的方法,我建议避免重新键入内容:
include 'db.php';
if(isset($_POST['submit'])){
$em = trim($_POST['email']);
if($em === '' || !preg_match('/^.{1,62}@.{1,62}'.[a-zA-Z]{2,4}$/', $em)){
$error = 'Please Enter a Valid Email';
}
else{
$sml = $db->query("SELECT email FROM survey_mail WHERE email='$em'");
if($sml->num_rows > 0){
$error = 'Sorry, this Email is Already in Use';
}
else{
$eml = $db->real_escape_string($em);
$db->query("INSERT survey_email (email) VALUES ('$eml')");
//the below leaves out the $doctype, beginning html tag and your $css
$dt = date('l, F j, Y, g:i:sa T', time());
$msg = "$doctype<title>Your Email Was Received</title><style type='text/css'>$css</style></head><body><a href='http://www.domain/wherever.php?em=someCrazyHashOrSomthing'>Please Click the Following Link to Confirm Your Email Address is Valid</a></div><div><b>Sent:</b> $dt <br /><b>Email:</b> $em <br /></div></body></html>";
mail($em, 'Thank You for Contacting Us', $msg, "From: name@domain.com'r'ncontent-type: text/html");
}
}
$sml->free(); $db->close();
}
else{
$em = 'example@example.com';
}
当然,现在您需要 PHP 来验证电子邮件中的链接信息。我不会为你做所有的工作,所以玩得开心。你可以考虑使用我的图书馆PHPglue。它将对数据进行多次检查,只需很少的工作,并且包括自动JavaScript错误,而无需了解JavaScript。
,所以我将描述你应该采取的步骤,以获得一个好的解决方案:
在 PHP 的数据访问层中,您应该运行一个查询,以检查数据库中是否有与此电子邮件一致的电子邮件地址。这样的东西应该在数据库服务器上运行(不过要注意SQL注入):
选择存在(从电子邮件 = "test@example.com"的用户中选择 NULL)
如果电子邮件存在,则显示错误
实现一个PHP函数,该函数将使用正则表达式测试电子邮件地址是否有效。使用谷歌找到一个不错的正则表达式。
如果电子邮件格式无效,则显示错误
在 Javascript 中实现电子邮件格式验证器(可选,但如果电子邮件地址无效,最好根本不发送请求),使用您在 PHP 函数中使用的相同正则表达式。
实现表单的 Submit 事件,以便仅在电子邮件地址通过 Javascript 验证时提交。
因此,当客户端使用表单时,您首先使用 Javascript 验证器检查电子邮件地址,并且仅在请求有效时才发送请求,在 PHP 中检查电子邮件格式,如果有效,请检查数据库中是否重复。处理可能的故障和成功方案。调试愉快。
require_once($_SERVER['DOCUMENT_ROOT'] . '/includes/system/init.php');
if(isset($_POST['submit'])) {
// get the email address
$email = strip_tags(trim($_POST['email']));
// see if email is empty or invalid
if($email == '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error_message = 'Please enter a valid email address';
} else {
// email is valid
$success = true;
// check if email address is already in database
$sql = mysql_query("SELECT email FROM users WHERE email='".mysql_real_escape_string($email)."'") or die(mysql_error());
$check = mysql_fetch_assoc($sql);
if($check > 0) {
// email address exists
$error_message = 'That email address exists';
} else {
// email address does not exist
mysql_query("INSERT INTO survey_email (email) VALUES('".mysql_real_escape_string($email)."' ) ") or die(mysql_error());
header('Location: /survey-email-confirm.php');
}
}
}
你应该使用 mysqli 或 PDO,因为 mysql 是折旧的。此外,切勿在没有先清理数据的情况下将数据放入数据库 - 即使您的 PHP 脚本创建了它。永远不要信任数据,尤其是来自用户输入的数据。
编辑:我误读了OP的问题。更新了新答案。