Jump to content

using code to use database tables


avatar.alex

Recommended Posts

Ok I can't find it anywhere or know what its called. But I have my user management  script coded for the most part. I have a table called groups and there are Standard users, mods, and Admin. If I wanted the page I choose to just be available for ADMIN how would I go about doing that?

Link to comment
https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/
Share on other sites

what does your table setup look like? I would need that to give an exact answer, but you wanna do a query like

$result = mysql_query("SELECT * FROM TABLE WHERE username = 'whatever variables holds username' AND rank='admin'");
if (mysql_num_rows($result) > 0){
//show page 
}

 

OK I thought it would have worked but it didn't I have

 

<?php 

$result = mysql_query("SELECT * FROM Users WHERE Group_ID = '2'");
if (mysql_num_rows($result) > 0){
?>
Admin page

<?php 
} else { 
header("Location: index.php"); die; 
}

?>

 

  A standard user has a group id of "1" an administrator has a group id of "2". If the user doesn't have a group id of 2 they should be located to the index page.

exactly why it wont work otherwise.

to get what group affilation a user belongs to, u need to pass both requirements. user_id and group_id

SELECT * FROM Users WHERE Group_ID = '2'

this just returns all users in group  2. so either you check each row to see if u find the users id

or u put both in the query

when the user logs in you have to set somethin like a session variable

 

$_SESSION['LOGIN'] = $login;

 

 

then:

$login=$_SESSION['LOGIN'];
$usergroup = mysql_query("SELECT usergroup FROM Users WHERE login='$login'");
$result=mysql_query($usergroup)

$group=mysql_result($result,0,"usergroup")
if ($group == 2)  {
   display page
}
else {
   redirect
}

this is my session

session_start();

//Global User Object Var
//loggedInUser can be used globally if constructed
if(isset($_SESSION["userCakeUser"]) && is_object($_SESSION["userCakeUser"])) $loggedInUser = $_SESSION["userCakeUser"]; else $loggedInUser = NULL;

So I could take the $_SESSION["userCakeUser"]; and = it to $login?

this is my session

session_start();

//Global User Object Var
//loggedInUser can be used globally if constructed
if(isset($_SESSION["userCakeUser"]) && is_object($_SESSION["userCakeUser"])) $loggedInUser = $_SESSION["userCakeUser"]; else $loggedInUser = NULL;

So I could take the $_SESSION["userCakeUser"]; and = it to $login?

 

Yes, if the 'userCakeUser' session variable is the same as what it'd be in the database.

I have, but the whole page is blank? Not even a error?

<?php 
$log = $_SESSION["userCakeUser"]
$usergroup = mysql_query("SELECT Group_ID FROM userCake_Users WHERE login='$log'");
$result=mysql_query($usergroup)

$group=mysql_result($result,0,"Group_ID")
if ($group == 2)  {
?>

Ok this is what I have my page is just blank. It won't even show anything on view source.

<?php 
$log = $_SESSION["userCakeUser"]
$usergroup = mysql_query("SELECT Group_ID FROM userCake_Users WHERE login='$log'");
$result=mysql_query($usergroup)

$group=mysql_result($result,0,"Group_ID")
if ($group == 2)  {
?>

when you define your session on login add

 

$_SESSION['groupID'] = mysql_result(mysql_query("SELECT Group_ID FROM userCake_users WHERE username = '$loggedinusername'"),0)

Where $loggedinusername is the username the person used to log in.

Then in the page you can use

<?php
if ($_SESSION['groupID'] >= 2)
{
?>Admin Page
<?php
}
else
{
header('Location: index.php');
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.