GrfxGuy79
GrfxGuy79

Reputation: 11

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.

Upvotes: 0

Views: 58

Answers (1)

kmoser
kmoser

Reputation: 9273

Your fetchSingle() function returns the result of fetch(), which will be either an associative array (if success), or FALSE (if failure).

You're checking if ($result == 1), which will never be true. You should be checking if (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"); 
}

Upvotes: 2

Related Questions