There is the following table:

enter image description here

With the following entries:

enter image description here

Accordingly, users who have the unit "admin" unit are administrators, with zero - no.

I check the code:

<?php require "auth.php"; $username = $_SESSION['login']; $admin = ("SELECT admin FROM loginparol WHERE login='$username'"); $result = $connection->query ($admin); if ($admin == 0) { header('Location: MainPage.php'); exit(); } if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { echo $row["admin"]; //Сделал вывод, чтобы самому понимать, какое число в $admin передаётся, тут всё верно } } 

Verification does not work, sends to "MainPage.php" in general all without parsing. If you do not write "== 0", but "! = 1", then it does not work at all. I understand that the error is due to the fact that in the if I incorrectly write the condition. Tell me, please, how it will be right, I myself can not guess, I study php and SQL in the course of project development.

  • Do you at all understand what you are doing? You have $admin - this is a query string and you compare it with 0 , of course it will be equal. Since it equates to type int , which will be equal to 0 . And the injection is provided. Read about the requests being prepared. - And
  • if ($result->fetch_assoc()[0]["admin"] == 0) { and you need to add a check for the existence of this array, otherwise if there is no user at all - there will be an error. - Manitikyl 10:35 pm

1 answer 1

In general, thanks to the user And, who left a comment, poked at the error and explained where to look for the answer. Redid the code like this:

  if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $admin1= $row["admin"] ; } } if ($admin1 == 0) { header('Location: MainPage.php'); exit(); } 

The essence of the error is that I initially compared the request with zero. In order for everything to work as I had intended, the value of the request had to be passed to some variable. What I did in the revised version of the code. I hope some newcomer like me will help this answer.

  • tinyint(1) type tinyint(1) - Bert