PHP - isset($_GET($var) not working

what im trying to do is build a database of certain machine loactions...

if...
1. no country AND no state is specified - i want to return all results
2. if a country is specified AND no state - i want to display the list of countries
3. if a state is specified, i want to display countries with that state in
4. anthing else.. display all results

im using the isset method to determine weather the two GET vars have been intialised... but the if statements arent functioning correctly...


if (isset($_GET['country_id'])) AND (isset($_GET['state_id']))
{
      $country_id=$_GET['country_id'];
      $state_id=$_GET['state_id'];
      $query="SELECT * FROM machine_locations WHERE country='$country_id' AND state='$state_id'";
}
else
{
      if (isset($_GET['country_id'])) AND !(isset($_GET['state_id']))
      {
            $country_id=$_GET['country_id'];
            $query="SELECT * FROM machine_locations WHERE country='$country_id'";
      }
      else
      {
            if (!(isset($_GET['country_id'])) AND !(isset($_GET['state_id']))) OR (!(isset($_GET['country_id'])) AND (isset($_GET['state_id'])))
            {
                  $query="SELECT * FROM machine_locations";
            }
            else
            {
                  $query="SELECT * FROM machine_locations";
            }
      }
}

$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_close();



thanks in advance,

sean
LVL 1
s34nAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

InteractiveMindCommented:
Should that not be && instead of AND ?

ie.,

if (isset($_GET['country_id'])) && (isset($_GET['state_id']))
                                            ^
0
InteractiveMindCommented:
And also || instead of OR:

if (!(isset($_GET['country_id'])) && !(isset($_GET['state_id']))) || (!(isset($_GET['country_id'])) && (isset($_GET['state_id'])))

etc.
0
InteractiveMindCommented:
As a whole:


if (isset($_GET['country_id'])) && (isset($_GET['state_id']))
{
     $country_id=$_GET['country_id'];
     $state_id=$_GET['state_id'];
     $query="SELECT * FROM machine_locations WHERE country='$country_id' AND state='$state_id'";
}
else
{
     if (isset($_GET['country_id'])) && !(isset($_GET['state_id']))
     {
          $country_id=$_GET['country_id'];
          $query="SELECT * FROM machine_locations WHERE country='$country_id'";
     }
     else
     {
          if (!(isset($_GET['country_id'])) && !(isset($_GET['state_id']))) || (!(isset($_GET['country_id'])) && (isset($_GET['state_id'])))
          {
               $query="SELECT * FROM machine_locations";
          }
          else
          {
               $query="SELECT * FROM machine_locations";
          }
     }
}

$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_close();
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

s34nAuthor Commented:
oops school boy error

but it still doesnt seem to be working
0
InteractiveMindCommented:
Well, the logic appears to be fine ..

Although, you could simplify this bit:

if (!(isset($_GET['country_id'])) && !(isset($_GET['state_id']))) || (!(isset($_GET['country_id'])) && (isset($_GET['state_id'])))
          {
               $query="SELECT * FROM machine_locations";
          }
          else
          {
               $query="SELECT * FROM machine_locations";
          }


to just:

$query="SELECT * FROM machine_locations";

(seeing as that's going to happen either way).


What output are you expecting, and what output are you actually getting ?
0
s34nAuthor Commented:
The full code that doesnt work (nothing generates on the browser window


<?
include("0_details.inc");

mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

if (isset($_GET['country_id'])) && (isset($_GET['state_id']))
{
     $country_id=$_GET['country_id'];
     $state_id=$_GET['state_id'];
     $query="SELECT * FROM machine_locations WHERE country='$country_id' AND state='$state_id'";
}
else
{
     if (isset($_GET['country_id'])) && !(isset($_GET['state_id']))
     {
          $country_id=$_GET['country_id'];
          $query="SELECT * FROM machine_locations WHERE country='$country_id'";
     }
     else
     {
          $query="SELECT * FROM machine_locations";
     }
}

$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_close();

echo "test"; // doesnt generate on the browser, so this line isnt being reached
?>




however, if i commented the if statements out like so...

<?
include("0_details.inc");

     $country_id=$_GET['country_id'];
     $state_id=$_GET['state_id'];

mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

     $query="SELECT * FROM machine_locations WHERE country='$country_id' AND state='$state_id'";
$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_close();

?>

this would work fine
0
sgtpredatorCommented:
Im looking at the first if statment and it looks like you might be closing the if statment before the and:

if (isset($_GET['country_id'])) AND (isset($_GET['state_id']))

Im a beginner as well but I thought it was supposed to look like this

if (isset($_GET['country_id']) AND isset($_GET['state_id']))

Again I am a beginner and it may work they other way, but that was the first thing that popped out at me.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
InteractiveMindCommented:
Yer, good catch — you're short of a closing bracket on all of your if statements.


<?
include("0_details.inc");

mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

if (isset($_GET['country_id'])) && (isset($_GET['state_id'])) )
{
     $country_id=$_GET['country_id'];
     $state_id=$_GET['state_id'];
     $query="SELECT * FROM machine_locations WHERE country='$country_id' AND state='$state_id'";
}
else
{
     if (isset($_GET['country_id'])) && !(isset($_GET['state_id'])) )
     {
          $country_id=$_GET['country_id'];
          $query="SELECT * FROM machine_locations WHERE country='$country_id'";
     }
     else
     {
          $query="SELECT * FROM machine_locations";
     }
}

$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_close();

echo "test"; // doesnt generate on the browser, so this line isnt being reached
?>



Well spotted, sgtpredator .  =)
0
s34nAuthor Commented:
ahh thankyou for that!

it seems php is a little more stricter than vB!

well done
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.