简体   繁体   中英

MySQL query not updating after but PHP shows no errors

This is for user activation.

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;
    }
}

Activation.php

if (isset($_GET['success']) === true && empty($_GET['success'])===true){
    echo 'Account activated!';
}
else if (isset($_GET['email'], $_GET['email_code']) === true){

    $email = trim($_GET['email']);
    $email_code = trim($_GET['email_code']);

    if(email_exists($email) === false){
        $errors[] = 'Oops, something went wrong!';
    }else if (activate($email, $email_code === false)){
        $errors[] = 'We have problems activating your account!';
    }
    if (empty($errors) === false){
        echo output_errors($errors);
    }else{
        header('Location:activate.php?success');
        exit();
    }
}else{
    header('Location:go.php');
    exit();
}

It says 'Account activated!' as I echoed but it didn't change the field in the table. It didn't activate at all basically. What is the problem here?

You have to change the function like this

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){
    if(mysql_query("UPDATE `users` SET `active` = 1 WHERE `email` = '$email'")){
        return true;
        }
  else{
    return false;
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM