Your HostICan Community  


Go Back   Your HostICan Community > Programming & Design > PHP / MySQL Assistance
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

PHP / MySQL Assistance We understand. Not all of you are experienced programmers, but some of you are highly skilled at coding. So we've set aside this forum so you can ask questions, and provide answers to the most commonly asked PHP / MySQL issues.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-28-2008, 10:53 PM
HunteRRosE's Avatar
Member
 
Join Date: Jan 2008
Location: Centreville, VA
Posts: 30
Default php IF statement problem, probably a newbie problem

This is driving me crazy.

I have a mysql db I'm trying to use to verify whether an account is an admin account or not, based on a bool value of 0 (not admin) or 1 (admin).

For whatever reason, my if statement isn't working correctly. Hopefully someone can help.

Basically I cant get the admin's to go to the admin page and the members to the members page. The members can somehow get to the admin page. These are literally just test pages I've setup that tell you which page your on.

Here's part of my code:

PHP Code:
$admin mysql_query('SELECT admin FROM users');

if (
$admin == 0) {
//then redirect them to the members area
header("Location: members.php");
}
else
{
//then redirect them to the admin area
header("Location: admin.php");

Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 05:37 AM
proxomos's Avatar
Junior Member
 
Join Date: Feb 2008
Posts: 1
Cool

PHP Code:
$admin mysql_query('SELECT admin FROM users'); 

if (
$admin == 0) { 
//then redirect them to the members area 
header("Location: members.php"); 

else 

//then redirect them to the admin area 
header("Location: admin.php"); 

I would do:

PHP Code:
$result mysql_query("SELECT admin FROM users");
list(
$admin)=mysql_fetch_row($result);

switch (
$admin){
case 
0:
   
header("Location: members.php");
   break;

case 
1:
  
header("Location: admin.php");
  break;

Hope that helps

Proxomos
Reply With Quote
  #3 (permalink)  
Old 04-25-2008, 02:38 PM
itwasntme's Avatar
Senior Member
 
Join Date: Sep 2007
Posts: 118
Default

The code is going to have to specify which user to select; otherwise, you're going to get unpredictable results.

Proxomos code modified to do so would look like:
Code:
$result = mysql_query("SELECT admin FROM users WHERE id=$user_id");
list($admin)=mysql_fetch_row($result);

switch ($admin){
case 0:
   header("Location: members.php");
   break;

case 1:
  header("Location: admin.php");
  break;
}
That's if you have a unique (primary key) field in your DB called "id" and have already declared the variable $user_id according to the login procedure to correspond to the current user. You'll need to change that part to match your field name.

Last edited by itwasntme : 04-25-2008 at 02:41 PM.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 09:42 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0

Copyright © 2003 - 2008 HostICan. All Rights Reserved.