I am having trouble with this code block:

 if($row['banned'] == 1) {

  header( 'Location: http://www.awsomechat.comuv.com/ban/userbanned.php' );

 } elseif ($row['banned'] == 2){
     header( 'Location: http://www.awsomechat.comuv.com/ban/userbanned.php' );

 }

Ive also tried this:

 if($row['banned'] == 1 or $row['banned']== 2) {    

 header( 'Location: http://www.awsomechat.comuv.com/ban/userbanned.php' );   
 }

But none of this works... I am trying to check if $row['banned'] equals 1 or two, and go to a specific page if the MySQL database has the value of 1 or 2 in the banned column. So, I want it to check if the column is equal to 1 or 2, then redirects to the page specified.

Recommended Answers

All 11 Replies

Member Avatar for LastMitch

@Djmann1013

if($row['banned'] == '1') {

 } elseif ($row['banned'] == '2'){

 }

I think you forgot to put ''(qoutes)

You don't ever use the word OR. Use this symbol || which means or. This symbol && means AND.

if($row['banned'] == 1 or $row['banned']== 2) {

}

What is the message you get when you run your code? Try putting values you're trying to check within single/double qoutes.

rotten69 didn't change your code for the OR, you cannot use OR in PHP, only in sql queries. So it should be

if($row['banned'] == 1 || $row['banned'] == 2) {
  header ('location: url');
}

You don't need speach marks around numbers/integers as suggested earlier either.

Have you echo'd $row['banned'] to ensure you actually have a value coming from the database?

Also what happens if they aren't banned, surely you should have an else statement following your if for cases like that?

I stand corrected - but, I have never once, in all my learning over the years, come across any PHP code that uses the actual words 'and' or 'or' instead of '&&' or '||'

I use it all the time for readability, but know they have different precedence.

they have different precedence.

True. && is before and - || is before or

:)

// Connect to server and select database.
$con = mysql_connect($host, $username, $password)or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$myusername = $_SESSION['username'];

$query = mysql_query("SELECT * FROM $tbl_name WHERE user_username='$myusername'") or die(mysql_error());
$row = mysql_fetch_array($query);

        if($row['banned'] == '1') {
         header( 'http://www.awsomechat.comuv.com/ban/userbanned.php' );
        } elseif ($row['banned'] == '2'){
         header( 'http://www.awsomechat.comuv.com/ban/userbanned.php' );
        }

It's still (ironically) not redirecting. I have it include(); in a different page, like so: include('/home/a8913488/public_html/ban/userban.php'); It takes the $_SESSION['username'] and stores it into a variable like this: $myusername = $_SESSION['username'];. I think the problem might be that it's not getting the session. But I don't know why.....It's included in the page WITH the session variable.... I just don't know..

As I put in my original post!

commented: Thanks +1

I am so stupid....I put the session_start(); at the worng place on the page....Thanks for all that helped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.