PHP MySQL - Can't get update table to work

I have an email verification after registration. I cannot get it to update the DB, I know the$_GET
is working (I echoed it just to see), but I cannot get it to continue. I am not sure what I am not seeing, so if anyone can shed some light, I would greatly appreciate it.
dbc is my database connection
if (isset($_GET['vkey'])) {
$vkey = $_GET['vkey'];
$db = new dbc;
$db->query("
SELECT user_verified
, user_vkey
FROM users
WHERE user_verified = 0
AND user_vkey = :user_vkey
LIMIT 1
");
$db->bind('user_vkey', $vkey, PDO::PARAM_STR);
$result = $db->fetchSingle();
if ($result == 1) {
// Validate Email
$db->query("
UPDATE users
SET user_verified = 1
WHERE user_vkey = :user_vkey
LIMIT 1
");
$db->bind("user_vkey", $vkey, PDO::PARAM_STR);
$run = $db->execute();
if($run){
echo "Account has been verified";
}else{
$db->error;
}
} else {
echo $_GET['vkey'];
echo "<br>This account is invalid or already verified";
}
} else { die("Something went wrong");
}
Here is the fetch function
public function fetchSingle()
{
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
Here is the DB table
CREATE TABLE `users` (
`user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) NOT NULL DEFAULT '',
`user_email` varchar(255) NOT NULL DEFAULT '',
`user_pw` varchar(255) NOT NULL DEFAULT '',
`user_dob` date NOT NULL,
`user_country` int(11) NOT NULL,
`user_state` int(11) NOT NULL,
`user_lvl` tinyint(1) NOT NULL DEFAULT '0',
`user_vkey` varchar(255) NOT NULL DEFAULT '',
`user_verified` tinyint(1) NOT NULL DEFAULT '0',
`user_date` date NOT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_email` (`user_email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The response I'm getting is in the else statement, "This account is invalid or already verified".
So I am guessing the issue is in the initial query.
Answer
Solution:
YourfetchSingle()
function returns the result offetch()
, which will be either an associative array (if success), or FALSE (if failure).
You're checkingif ($result == 1)
, which will never be true. You should be checkingif (is_array($result))
. So your corrected code would be:
if (isset($_GET['vkey'])) {
$vkey = $_GET['vkey'];
$db = new dbc;
$db->query("SELECT user_verified,user_vkey FROM users WHERE user_verified = 0 AND user_vkey = :user_vkey LIMIT 1");
$db->bind('user_vkey', $vkey, PDO::PARAM_STR);
$result = $db->fetchSingle();
if (is_array($result)) { // If row was retrieved successfully
// Validate Email
$db->query("UPDATE users SET user_verified = 1 WHERE user_vkey = :user_vkey LIMIT 1");
$db->bind("user_vkey", $vkey, PDO::PARAM_STR);
$run = $db->execute();
if($run){
echo "Account has been verified";
}else{
$db->error;
}
} else {
echo $_GET['vkey'];
echo "<br>This account is invalid or already verified";
}
} else { die("Something went wrong");
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: cannot use isset() on the result of an expression (you can use "null !== expression" instead)
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.