PHP - 无法更新MySQL表

时间:2018-01-12 02:16:01

标签: php mysqli

我尝试创建电子邮件验证但由于某些原因,我无法更新 MySQL 表。我已经尝试了各种不同的方式,但我似乎无法更新它。

<?php
$connect = mysqli_connect("x", "x", "x", "x");

if(isset($_GET['email']) && !empty($_GET['email']) AND isset($_GET['hash']) && !empty($_GET['hash'])){
    // Verify data
    $email = mysqli_escape_string($_GET['email']); // Set email variable
    $hash = mysqli_escape_string($_GET['hash']); // Set hash variable

    $statement = mysqli_prepare($connect, "SELECT * FROM users WHERE email = ? AND hash = ?"); 
    mysqli_stmt_bind_param($statement, "ss", $email, $hash);
    mysqli_stmt_execute($statement);
    mysqli_stmt_store_result($statement);
    $count = mysqli_stmt_num_rows($statement);
    mysqli_stmt_close($statement); 

    if($count < 1){
    // We have a match, activate the account

        $statement = mysqli_prepare($connect, "UPDATE users SET active ='1' WHERE email=? AND hash=?"); 
        mysqli_stmt_bind_param($statement, "ss",$email, $hash);
        mysqli_stmt_execute($statement);
        mysqli_stmt_store_result($statement);
        $count = mysqli_stmt_num_rows($statement);
        mysqli_stmt_close($statement); 

        echo '<div class="statusmsg">Your account has been activated, you can now login</div>';
    }
    else{
    // No match -> invalid url or account has already been activated.
        echo '<div class="statusmsg">The url is either invalid or you already have activated your account.</div>';
    }
}
else{
    // Invalid approach
    echo '<div class="statusmsg">Invalid approach, please use the link that has been send to your email.</div>';
}

?>
    <!-- stop PHP Code -->

2 个答案:

答案 0 :(得分:0)

行未更新的可能原因之一是因为您正在检查小于1的行数,这将是错误的,因为您期望SELECT语句中的某些行。

if($count < 1){

相反,它应该是

if($count == 1){

答案 1 :(得分:0)

在您的IF挑战中,您正在查看数据库中是否存在具有该哈希和rmail的条目。但是应该有一个还没有活跃的。所以:db中应该只有1个条目。

if($count == 1){

这应该可以解决问题。

你还应该进一步寻找

AND active=0

在第一个SQL语句中,一旦激活uset,就可以避免2次激活或从数据库中删除哈希值。