samedi 29 août 2015

PHP SQL IF statement problems

Ok so I'm trying to pull some data from my SQL database and use it in an IF statement.

I have a database called DB_Default and I have a table called Users

Inside Users I have all the normal columns such as id, username, password but I also have a column called isadmin.

I want to be able to do a mysql query through PHP selecting all users with the username $_SESSION[username] and isadmin = 1.

What I aim on doing is including a few navigation links for escalated privileged users only. So as the page that the code is going to be put into is for logged in users only I thought right, lets use the already defined username session i,e if my sessions username was set to Admin.

the statement should be

$result = mysql_query("SELECT * FROM users WHERE user_name='" . $_SESSION["user_name"] . "' and isadmin = '". $admin."'");

I have set $admin = "1"; so that it should only be able to return results if the user logged in has isadmin set to 1.

Instead I'm either having the navigation links show to any user regardless of their isadmin status or not at all.

To be honest the code is very messy as it's 5:40am and I haven't been coding for a while so quite rusty so I'm more than aware of how easy this should be of a fix.

I'm 99% sure it has to do with the fact I just set $admin = "1"; but for the life of me can't figure out where I've gone wrong. Going to give it a rest for today and come back tomorrow. Hopefully by then someone will have posted a resolution and obviously I'll vote up the best answer otherwise I'll give the code a lookover and see if I can't fix it myself!

Thanks!

Aucun commentaire:

Enregistrer un commentaire