数据库没有更新值,空白页

时间:2014-10-21 18:11:07

标签: php mysql pdo

好的,我再来一次。我提出了类似于这个的问题,但不同之处在于没有形式。

我正在尝试验证用户帐户,因此我向用户的电子邮件地址发送验证链接,当他们点击链接时我需要数据库将数据库中的is_active列更新为1并清除URL的令牌

这是我的PHP代码,应该在用户点击链接时执行:

require("../config.php"); //connects to the database

$sql = "SELECT activation_expiration, is_active FROM users WHERE activation = :token";
$stmt = $db->prepare($sql);
$stmt->bindParam(":token", $_GET['token']);
$stmt->execute();
$result = $stmt->fetch();

// Is the user active?
$is_active = $result['is_active'];

//Get Token Expiration Date
$tokenExpiration = $result['activation_expiration'];

// Get current DateTime
$now = new DateTime();
$currentDate = $now->format('Y-m-d h:i:s a') . "\n";

// This function will update the user to active
function updateActivation($dbHandler){
if($tokenExpiration > $currentDate) {
if(isset($_GET['email']) && !empty($_GET['email']) AND isset($_GET['token']) && !empty($_GET['token'])){

    $email = $_GET['email']; // Set email variable 
    $token = $_GET['token']; // Set token variable          

            $query_params = array(
                ':user' =>  $email,
                ':token' => $token,
                ':emptyToken' => '',
                ':active' => 1
            ); 

            $dbHandler->beginTransaction();
            $sql = "UPDATE users SET is_active = :active, activation_expiration = :emptytoken WHERE username = :user AND activation = :token";
            $stmt = $dbHandler->prepare($sql); 
            $result = $stmt->execute($query_params);
            $dbHandler->commit(); 

            $account_verified = 'Account verified! Please log in.';
            $_SESSION['account_verified'] = $account_verified;

            header("Location: ../index.php"); 
            die("Redirecting to index.php");    

} else {
    $dbHandler->rollback();
}
} else {
header("Location: ../forgot-password/token-expired.php"); 
die("Redirecting to token-expired.php"); 
}
}

// This will run the function if the user is not active
if($is_active === 0){
try {
    updateActivation($db);
} catch (Exception $e){
    $error =  'The following error occured: <br/>'.$e->getMessage();
}   
}elseif($is_active === 1){
echo "You have already verified your account!";
}

我没有收到错误,$ _GET值存储在变量中,但由于某种原因它没有运行更新命令。

所以,我的问题是你的代码中有什么问题吗? 我错过了什么? 总的来说,如何让更新脚本正常工作?

  

编辑:示例网址http://www.mystreetlife.com/my-home/admin/users/verify.php?email=email@gmail.com&token=8d0522b85c9c16c3dfy349d02324058b

1 个答案:

答案 0 :(得分:2)

好的,所以我终于开始工作了。

我将我的功能代码更改为:

// This function will update the user to active
function updateActivation($dbHandler){

if($GLOBALS['tokenExpiration'] > $GLOBALS['currentDate']) {

    $email = $_GET['email']; // Set email variable 
    $token = $_GET['token']; // Set token variable
    $emptyToken = ''; // Set variable to empty the activation token in the database
    $emptyExpiration = ''; // Set variable to empty the activation expiration token in the database
    $active = 1; // Set variable to update user to active in database

    try {
            $query = "UPDATE users SET is_active = :active, activation = :emptytoken, activation_expiration = :emptyexpiration WHERE username = :user AND activation = :token";
            $stmt = $dbHandler->prepare($query);
            $stmt->bindParam(":user", $email); 
            $stmt->bindParam(":token", $token); 
            $stmt->bindParam(":emptytoken", $emptyToken);
            $stmt->bindParam(":emptyexpiration", $emptyExpiration);
            $stmt->bindParam(":active", $active);
            $stmt->execute();

            header("Location: ../index.php"); 
            die("Redirecting to index.phpp"); 
    } catch (Exception $e) {
        echo '<strong>The following error occured:</strong> '.$e->getMessage();
    }           

} else {
    header("Location: ../forgot-password/token-expired.php"); 
    die("Redirecting to token-expired.php"); 
}
}

然后运行该函数:

// This will run the function if the user is not active
if ($is_active == 0) {
try {
    updateActivation($db);
} catch (Exception $e){
    echo '<strong>The following error occured:</strong> '.$e->getMessage();
}   
}