Take a look at the following line
and consider the case when you get no rows returned from the database. Your code would then in principle look like
true, because the exclamation-mark is the not-operator - it reverses it.
if (1) would also be true, but
if (0) would be false (and skip to the
else-block of your statement).
The solution is to simply remove this
!, as your logic is currently reversed. So that line would look like
To make your code even more readable, you can check if it's greater than zero, it basically does the same thing, but it's quicker to understand exactly what's going on. It's a personal preference though.
if (mysqli_stmt_num_rows($query) > 0)