I\'m doing some exercises from Beginners PHP & MySQL by Mr. Tucker.
On his example everything works fine, but on my PC there is an error:
You are trying to access the variable $passwordRetrieved
when it has not yet been given a value. The access is done here:
echo "passwordRetrieved = ".$passwordRetrieved."
";
The variable would be set just above:
while ($row = mysql_fetch_array($tUser_SQLselect_Query, MYSQL_ASSOC)) {
$passwordRetrieved = $row['password']; // VARIABLE SET HERE
}
The important thing is that the variable only gets set if the query returns a matching row (i.e., on a successful login). If the login is not valid, the variable is not set.
To check if a variable is set without getting a notice, you would use either isset
or empty
(there are subtle differences, but as a rule of thumb you can use either most of the time). Your code actually already does this just below:
// Checking if $passwordRetrieved has been set using empty()
if (!empty($passwordRetrieved) AND ($password == $passwordRetrieved)) {