PDO with MySQL does not work when activating email

Ok, so I am setting up the activation page using $ _GET [] via the link that the server sends to the user.

Here is my activation page.

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

I believe that everything is in order, the problem is in my function 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; } 

At this moment, I'm just trying to get something back, but it is not.

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

But I can’t translate it.

Any help would be appreciated, thanks.

I thought I would add that this does not return any errors, mainly because I have not set anything yet to return it.

EDIT:

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

Then the function

 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; } } 
+4
source share
1 answer
 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(); } } 
+2
source

Source: https://habr.com/ru/post/1479860/


All Articles