php – PDO与MySQL无法在电子邮件激活

好的,所以我使用服务器通过电子邮件发送给用户的链接中的$_GET []来设置激活页面.

这是我的激活页面.

if (isset($_GET['success']) && $_GET['success'] == false) {
        echo 'Your account has been activated, please login to continue.';
    } else if (isset($_GET['email'], $_GET['email_code']) === true) {
        $email          = trim($_GET['email']);
        $email_code     = trim($_GET['email_code']);

        if (email_exists($db, $_GET['email']) == false) {
            $errors[] = 'This email address hasn\'t been registered with us.';
        } else if (activate($db, $email, $email_code) === false) {
            $errors[] = 'We had problems activating your account, please contact an Administrator.';
        }

        if (empty($errors) === false) {
            echo output_errors($errors);
        } else {
            header('Location: activate.php?success');
            exit();
        }
    } else {
        header('Location: index.php');
    }

我相信没问题,问题在于我的函数activate()

    function activate(PDO $db, $email, $email_code) {
$stmt = $db->prepare("SELECT COUNT (`id`) FROM `users` WHERE `email` = :email AND `email_code` = :email_code AND `active` = 0");
$stmt->bindValue(':email', $email);
$stmt->bindValue(':email_code', $email_code);
$stmt->execute();

$row = $stmt->fetch(PDO::FETCH_OBJ);

return $row ? $row->type : 0;
}

在这一刻,我只是想让它返回一些东西,但事实并非如此.

我真正需要的是这样做.

function activate($email, $email_code) {
$email          = mysql_real_escape_string($email);
$email_code     = mysql_real_escape_string($email_code);

if (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `email` = '$email' AND `email_code` = '$email_code' AND `active` = 0"), 0) ==1) {
    mysql_query("UPDATE `users` SET `active` = 1 WHERE `email` = '$email'");
    return true;
} else {
    return false;
}
}

但我不能完全翻译它.

任何帮助将不胜感激,谢谢.

我以为我会添加这不会返回任何错误,主要是因为我还没有正确地输入任何错误,因为它返回一个.

编辑:

else if (activate($db, $email, $email_code) === 0) {
            $errors[] = 'We had problems activating your account, please contact an Administrator.';
        }

然后功能

function activate(PDO $db, $email, $email_code) {
$sql  = "SELECT `active`, `email_code` FROM `users` WHERE `email` = '?'";
$stmt = $db->prepare($sql);
$stmt->execute(array($email));
$row  = $stmt->fetch();
if ($row && $row['active'] == $email_code && !$row['active'] ) {
    $sql  = "UPDATE `users` SET `active` = 1 WHERE `email` = '?'";
    $stmt = $db->prepare($sql);
    $stmt->execute(array($email));
    return $stmt->rowCount();
} else {
    return 0;
}
}

最佳答案

function activate(PDO $db, $email, $email_code) {
    $sql  = "SELECT active, email_code FROM users WHERE email = ?";
    $stmt = $db->prepare($sql);
    $stmt->execute(array($email));
    $row  = $stmt->fetch();
    $if ($row && $row['active'] == $email_code && !$row['active'] )
        $sql  = "UPDATE users SET active = 1 WHERE email = ?");
        $stmt = $db->prepare($sql);
        $stmt->execute(array($email));
        return $stmt->rowCount();
    }
}
点赞