Jump to content


Photo

Problem with if & else statements (I think)


  • Please log in to reply
2 replies to this topic

#1 amp2000

amp2000
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 02 April 2003 - 07:35 PM

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

#2 shivabharat

shivabharat
  • Members
  • PipPipPip
  • Advanced Member
  • 371 posts
  • LocationChennai, India

Posted 02 April 2003 - 10:58 PM

//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!!
[/quote]
Knowledge --- Reading Enriches Mind But Sharing Enhances It.[br][br]Note: Before you request help enusre that you have had a look at the tutorials @phpfreaks

#3 amp2000

amp2000
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 04 April 2003 - 10:21 AM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users