There are a lot of very similar, near identical in fact, questions to this one and I have read through them all before posting this to see if the solutions therein could apply to my case, but alas they have not so far.
I'm using PHP 5.6 and MariaDB as the SQL DB.
As part of an email verification process the user clicks a link that contains their email address and a verification code that if matched in the DB sets the 'approved' field to 'Yes'. Very straightforward. The URL is formatted https://www.myserver.com/verify.php?user=email@address.com&auth=ri5934ij4jjo49
The PHP $_GET's these fields, queries the DB for a match and if found sets the 'approved' field to 'Yes', displays a message, then redirects and if not it displays a message then redirects. Also there's a redirect if no data is passed in so someone doesn't accidentally get stuck on this page.
The problem is, SQL UPDATE is executed even when the condition is false and the else gets triggered. As you'll see in the code I've left in the error checking and flow checking print_r and echo statements and exit()'s before the redirect so I can read the output and it appears to be flowing correctly. However the darn UPDATE gets run even when it shouldn't.
<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);
if(isset($_GET['auth']) && isset($_GET['user'])){
$auth=$_GET['auth'];
$user=$_GET['user'];
echo($auth.'<br />'.$user.'<br />');
$connect = mysqli_connect("server", "username", "password", "db") or die(mysqli_error($connect));
$verify = mysqli_query($connect, "SELECT * FROM users WHERE email='$user' AND verification_code='$auth'") or die(mysqli_error($connect));
$verify2 = mysqli_fetch_array($verify);
print_r($verify2);echo('<br />');
if(mysqli_num_rows($verify)==1){
mysqli_query($connect, "UPDATE users SET approved='Yes', verification_code='' WHERE email='$user' AND verification_code='$auth'") or die(mysqli_error($connect));
echo('Approved');exit();
echo('<script type="text/javascript">alert("Your registration has been approved, please log-in");window.location.href = "log-in.php";</script>');
} else {
echo('Not approved');exit();
echo('<script type="text/javascript">alert("There was a problem verifying your email, please try again, re-register or contact us for help");window.location.href = "log-in.php";</script>');
}
} else {
header('Location: log-in.php');
}
?>
');? Can you run the query straight on the table and see the result? – Grumpy Apr 12 '21 at 09:20