Your main error is that you call the query()
method twice. Both $conn->query
and mysqli_query()
are the same thing, and when you pass the result of one to the other you get this cryptic error.
Another issue is that your connection for mysqli is not up to the standards. See https://phpdelusions.net/mysqli/mysqli_connect to learn more about connecting. You should also enable error reporting with mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
instead of manually checking for errors with or die(mysqli_error($conn));
You should use parameterized prepared statements instead of manually building your queries. They are provided by PDO or by MySQLi. Never trust any kind of input! Even when your queries are executed only by trusted users, you are still in risk of corrupting your data.
Never store passwords in clear text or using MD5/SHA1! Only store password hashes created using PHP's password_hash()
, which you can then verify using password_verify()
. Take a look at this post: How to use password_hash and learn more about bcrypt & password hashing in PHP
I haven't fixed all of your issues, I leave that as an exercise for you, but this should act as a guide on what the proper mysqli code should look like.
<?php
session_start();
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli("localhost", "root", "", "...");
$conn->set_charset('utf8mb4');
$stmt = $conn->prepare('SELECT * FROM users WHERE username=? AND password=? AND isadmin=1');
$stmt->bind_param('ss', $_POST["username"], $_POST["password"]);
$stmt->execute();
$result = $stmt->get_result();
$firstRow = $result->fetch_assoc(); // See https://phpdelusions.net/mysqli/check_value
if ($firstRow) {
$_SESSION["admin"] = $_POST["username"];
exit(header("Location: index.php")); // Always `exit()` after `header('Location: ...');`
} elseif (!$result) {
$_SESSION["gatekeeper"] = $_POST["username"];
exit(header("Location: index.php")); // Always `exit()` after `header('Location: ...');`
}