Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Admin Level Access 1

Status
Not open for further replies.

danno74

IS-IT--Management
Nov 13, 2002
295
US
Greetings,

I have a php front end that collects survey results. In the MySQL table, for each user I have a userlevel setting. I would like users that have a level >= 2 to be able to see the admin features and user stats. I'm a newb with this stuff and can't figure it out, not sure what function to use.

Thank you for your help.

- Dan
 
at it's simplest you would store the admin routines in a separate php file. at the top of the file you would query the user's admin level and, if it's not high enough, kill the script using die() or exit().
 
How would you write the query? I know how to display items, like for instance:
Code:
<?php

// counts the number of surveys the person who has logged in has completed

$query = "SELECT COUNT(login) FROM survey WHERE login='" . $_SESSION['username'] . "'";

$surveys_taken = mysql_query($query) or die (mysql_error());

// print results

while($row = mysql_fetch_array($surveys_taken)){
			echo "<p align=center><b2>You have completed ". $row['COUNT(login)'] ." surveys.";
			echo "<br /></b2>";
}
?>

I don't know how to write it so it tells the script:

if $login >= 2
include("admin.php");

I don't know the correct way to do this.... :(

- Dan
 
Code:
$query = "SELECT adminLevel, count(Login) as num FROM survey WHERE login='" . $_SESSION['username'] . "'";

$surveys_taken = mysql_query($query) or die (mysql_error());

// print results
if (mysql_num_rows($surveys_taken)) == 0 {
// print something about having no records
} else {
  $row = mysql_fetch_array($surveys_taken);
  echo "<p align=center><b2>You have completed $row[num] surveys.";
  echo "<br /></b2>";
  if ($row['adminLevel'] > 2) {echo "you are an admin user";}
}
 
Thanks JP... I'll see if I can incorporate this in my page.
 
It's not working. Get the following error in the log:

PHP Parse error: syntax error, unexpected T_IS_EQUAL in /web/shtml-docs/obcr/portal/inc/admin.php on line 8, referer:
I tried to water it down to the following:

Code:
<?php

$query = "SELECT userlevel FROM survey WHERE login='" . $_SESSION['username'] . "'";

$surveys_taken = mysql_query($query) or die (mysql_error());

// print results
echo $surveys_taken;
	
	
?>

I get the following error on the page:

Unknown column 'userlevel' in 'field list'

Of course, I probably did something wrong since I don't know what I'm doing.
 
well. what is the name of the column in which you store the userlevel? the colname 'userlevel' in the query obviously needs to reflect the actual column name.

further you cannot echo the results of a query like that. mysql_query provides a resource handle which you can then use to fetch rows and columns of the resultset. you would need to use mysql_fetch_array or mysql_fetch_assoc and then use print_r on the result (as the result will be an array).

i cannot debug the first error for sure because i don't know the precise code that you are using. but i'd guess that this line may be throwing a curve ball and needs to be fixed as shown

Code:
if (mysql_num_rows($surveys_taken)[red][s])[/s][/red] == 0[red])[/red] {
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top