Jump to content

Problem with if & else statements (I think)


amp2000

Recommended Posts

Hi

I\'m having a few problems with getting a part of a script working.

Here is the relevant code


<?php

include("config.php");

include("header.php");

$dbh = mysql_connect ($dbhost, $dbuname, $dbpass) or die ( \'I cannot connect to the database because: \' . mysql_error());

mysql_select_db ($dbname) or die (mysql_error());



// Perform query on travel table

$query = "SELECT *  FROM travel";

$result = mysql_query($query) or die("Error:" .mysql_error());



//Authentication, user will not see results unless logged on

   global $user, $sitename;

   cookiedecode($user);

   $username = $cookie[1];

   if ($username == "") {

#        $username = "Anonymous";



   echo "You need to Logon";

   exit;

}

else

{

echo "OK, Your logged on, now checking to see are you a directorate";



//Perform query on names of directorates

$query2 = "SELECT name  FROM managers";

$result2 = mysql_query($query2) or die("Error:" .mysql_error());

while ($row2 = mysql_fetch_row($result2)) {

if (!$username == $row2[0]) {

echo "Your logged on but your not a directorate, bye!";

exit;

} else {

echo "<br>";

echo \'Your a Directorate!\';

}}}



echo OpenTable();

// Retrieve values

while ($row = mysql_fetch_row($result)) {

###### Rest of Script goes here #######

Now my problem is with this part (I think)


$query2 = "SELECT name  FROM managers";

$result2 = mysql_query($query2) or die("Error:" .mysql_error());

while ($row2 = mysql_fetch_row($result2)) {

if (!$username == $row2[0]) {

echo "Your logged on but your not a directorate, bye!";

exit;

If I am not logged on to my PHPnuke site I get the error \"You need to Logon\" & it exits as expected, this part works fine.

I want it so that if a user is logged on, it checks to see is their name in the managers table & if it\'s not they get the error \"Your logged on but your not a directorate, bye!\".

The problem is any user who is logged on to the site is told Your a Directorate! when they are not in my managers table :cry:

Can one of you experts take a look at my code & tell me if I\'m doing something obviously wrong :wink:

 

Also, notice the way I have added a 2 after the variable names for the 2nd query, do I need to do that :?:

 

Thanks in advance for any replies :D

Link to comment
Share on other sites

//Perform query on names of directorates 

$query2 = "SELECT name  FROM managers"; 



$result2 = mysql_query($query2) or die("Error:" .mysql_error()); 



while ($myrow = mysql_fetch_row($result)) 

{



[quote]Now in the myrow supply the correct row value in which u havethe name field [/quote]

$name = myrow[1];



$a = strcmp(\'$username\',\'$name\');



         if ($a == \'0\') 

         {

               echo "<br>"; 

               echo \'Your a Directorate!\'; 



         }    else



        { 

                echo "Your logged on but your not a directorate, bye!"; 

                exit; 



         }

}

 

Hope this helps!!

Link to comment
Share on other sites

Thanks for the reply :wink:

Your code didnt work though, well it did work but when I change the name of the directorate in my managers table the script fails to see the changes I made.

Here is the code I finally got to work for me :D


   global $user, $sitename;

  cookiedecode($user);

$username = $cookie[1];



$query2 = "SELECT name FROM managers WHERE name = \'$username\'";

$result2 = mysql_query($query2) or die("Error:" .mysql_error());

$result2_rows = mysql_num_rows ($result2);



if ($result2_rows == 0) {

echo "Your logged on but your not a directorate, bye!";

exit;

}

 

Cheers

amp2000

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.