首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >验证mysql列数据并在该行中查找匹配的电子邮件?

验证mysql列数据并在该行中查找匹配的电子邮件?
EN

Stack Overflow用户
提问于 2013-03-14 20:07:08
回答 2查看 1.6K关注 0票数 2

嗨,我想知道是否有人可以帮助我,我基本上有一个注册表,用户可以注册和每个注册是一个唯一的验证码生成到数据库中,并在电子邮件发送给用户时,他们注册。

我现在正在进行第二阶段的工作,用户点击他们电子邮件中的链接,然后被带到verification.php

在此页面上,用户必须键入他们在电子邮件中收到的验证码/注册码。我这里的脚本应该查找验证码,并确保它与数据库中的验证码匹配。

我想要做的是以某种方式告诉我的查询,如果验证码/注册码与我们在数据库中的验证码/注册码是正确的,那么发送一封确认电子邮件到我们为匹配验证码的用户持有的电子邮件地址。(因此,属于具有相同验证码的行的电子邮件。)

我的桌子看起来像这样

代码语言:javascript
复制
id           email               registration_code (verification code)
1      example@email.com                    5093dkfd

目前,我的查询搜索数据库中的注册码,但我不知道如何让它找到与注册码匹配的电子邮件并发送电子邮件。这一点我需要帮助。谁能给我指个正确的方向,

另外,我希望查询能告诉用户他们输入的代码是否正确,即使没有,它也会告诉用户他们输入的代码是否正确。

代码语言:javascript
复制
/**
 * ShuttleCMS - A basic CMS coded in PHP.
 * Verification Code Check - Used to confirm a user's verification code
 * 
 */
define('IN_SCRIPT', true);
// Start a session
session_start();

//Connect to the MySQL Database
include 'includes/_config/connection.php';


/*
 * Checks form field for verification code, then where verification code has email, send email to recipient     
 */

if ($_POST['verificationcode']=='') {
        header('Location: verification.php?err=1');
}
if(get_magic_quotes_gpc()) {
        $verificationcode = htmlspecialchars(stripslashes($_POST['verificationcode']));
} 
else {
        $verificationcode = htmlspecialchars($_POST['verificationcode']);

}


/*
 * Checks if the verification code exists and look for email in that row which belongs to that verification code to send email out.
 */

$sql = "SELECT COUNT(*) FROM ptb_registrations WHERE registration_code = '$verificationcode' AND '$verificationcode' MATCHES email";
$result = mysql_query($sql)or die('Could not find member: ' . mysql_error());

if (!mysql_result($result,0,0)>0) {
    header('Location: verification.php?err=2');
}

然后我们发送一封确认电子邮件:

代码语言:javascript
复制
/*
         * Email out the infromation
         */


    (EMAIL BODY)
    YOUR code was matched and you are now registered etc.
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-14 20:19:08

像这样的东西应该是有效的:

代码语言:javascript
复制
$sql = "SELECT email FROM ptb_registrations WHERE registration_code = '$verificationcode'";
$result = mysql_query($sql)or die('Could not find member: ' . mysql_error());
if (mysql_num_rows($result) == 0) {
   header('Location: verification.php?err=2');
} else {
  $row = mysql_fetch_array($result);
  $email = $row['email'];
}
票数 1
EN

Stack Overflow用户

发布于 2013-03-14 20:25:36

首先,您应该知道mysql_*函数已经被弃用,您应该使用其他MySQL库之一,比如mysqli或PDO (它支持多种SQL库)。

其次,您应该为您选择的mysql库使用适当的转义函数。

要选择匹配的电子邮件,请使用SELECT email FROM ...

要检查结果,您可以从num_rows函数(例如,mysql库中的mysql_num_rows或mysqli中的num_rows属性)开始,查看它是否找到匹配的电子邮件地址,如果找到匹配,则获取实际的行内容以获取电子邮件。然后使用PHP向该地址发送电子邮件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15408852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档