当用户多次点击电子邮件激活链接时,我希望显示正确的消息

时间:2012-08-26 20:05:15

标签: php mysqli

我正在为我的A-Level做一个编程课程,我遇到了问题。我希望你们其中一个人可以帮助我。

我正在设置成员系统,目前我处于电子邮件激活阶段。使用php和mysqli编码与连接的数据库,我已经能够设置电子邮件激活链接工作,以便当用户点击激活链接时,它通过将“活动”数据库字段设置为“0”来激活用户的帐户到'1'。

我唯一的问题是,如果用户再次点击电子邮件中的激活链接,因为用户已经处于活动状态,我希望它显示消息“此帐户已被激活”;。这样用户就无法再次激活帐户。

但我无法显示此消息。即使用户已激活帐户,它仍会显示“帐户已激活”消息。有谁知道我做错了什么?

以下是我的代码:

<?php
$user_to_be_activated  =   $_GET['user'];
$code_to_be_matched   =   $_GET['code'];
$code_activated = 1;

// don't use $mysqli->prepare here
$query = "SELECT TeacherUsername, Active, Code FROM Teacher WHERE TeacherUsername = ? AND Code = ? ";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("ss",$user_to_be_activated, $code_to_be_matched);
// execute query
$stmt->execute(); 
// get result and assign variables (prefix with db)
$stmt->bind_result($dbTeacherUsername, $dbActive, $dbCode);
//get number of rows
$stmt->store_result();
$counting = $stmt->num_rows();

if($counting == '1')
{

    if($dbActive == '1')
    {
        echo "This Account has already been Activated";
    }
    else{

    $updatesql = "UPDATE Teacher SET Active = ? WHERE TeacherUsername = ? AND Code = ?";                                            
    $update = $mysqli->prepare($updatesql);
    $update->bind_param("sss", $code_activated, $user_to_be_activated, $code_to_be_matched);
    $update->execute();
    $update->close();

    echo "Account is Activated";
}
}
else
{
    echo "The Code and Username doesn't match! Account is not Activated.";
}
?>

1 个答案:

答案 0 :(得分:0)

<?
$stmt->store_result();
$counting = $stmt->num_rows();

if($counting == '1')
{
    $stmt->fetch(); //added fetch here
    if($dbActive == '1')
    {
        echo "This Account has already been Activated";
    }
    else{

    $updatesql = "UPDATE Teacher SET Active = ? WHERE TeacherUsername = ? AND Code = ?";                                            
    $update = $mysqli->prepare($updatesql);
    $update->bind_param("sss", $code_activated, $user_to_be_activated, $code_to_be_matched);
    $update->execute();
    $update->close();

    echo "Account is Activated";
}
}
else
{
    echo "The Code and Username doesn't match! Account is not Activated.";
}
?>