I have php/sql code from a guide that does not echo the results it is suppose too. It is suppose to echo 2 results when checking the db. Admin is in the db and John is not so one is true and the other is false. The main connection is working so I can not figure out why it is not working. Thanks for your help.
echo “Person 1 with username of Admin and password of Password logged in: “.checkLogin(‘Admin’, ‘Password’, $db).” ”;
echo “Person 2 with username of John and password of 1234 logged in: “.checkLogin(‘John’, ‘1234’, $db).” ”;
echo “ ”;
?>
Well according to the code in the book that is all that is needed, it is suppose to check both those names and return true or false that they exist in the db. Admin exists so that is the true value, John does not exist in the db so that is false.
if ($statement->num_rows == 1) does this not return the value of 1 if true???
Ok that did something lol, it returned “false” on both echo’s and I know it is connecting to the db and that Admin is in there as a user. So admin should have returned “true”
Connected successfully
Person 1 with username of Admin and password of Password logged in: False
Person 2 with username of John and password of 1234 logged in:False
If you are using a book or whatever that uses SHA1 you need to toss it and learn from something that uses current coding practices. Instead of banging your head on this I suggest you use your time to learn PDO. Here is a good tutorial to get you going https://phpdelusions.net/pdo
Yes I realize that it is old but this is what they want me to learn from and as I told them the book code is wrong and does not work, but no one wants to admit that so I am trying to work out what is not working
Well, benanamen makes some good points, so keep them in mind. But you can’t learn everything at once I suppose, and even though learning practices that will need to be unlearned later does seem foolish, hopefully there is good reason for this.
Anyway, you didn’t show the INSERT code, so I don’t know, but I suspect the query isn’t returning any results because the password isn’t being found. To test this hypothesis, try
.....
// $query = "SELECT * FROM RegisteredUsers WHERE Username = ? AND Password = ? LIMIT 1";
$query = "SELECT * FROM RegisteredUsers WHERE Username = ? LIMIT 1"; // name only
if($statement = $db->prepare($query)) {
// $statement->bind_param('ss', $uname, $newpword);{
$statement->bind_param('s', $uname); // name only
.....
Then if that works, try similar for the password only.