1

我正在为我的 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.";
}
?>
4

1 回答 1

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.";
}
?>
于 2012-08-26T20:21:39.800 回答