We help IT Professionals succeed at work.

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

Medium Priority
1,292 Views
Last Modified: 2013-11-18
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
Comment
Watch Question

Top Expert 2006

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

ie.,

if (isset($_GET['country_id'])) && (isset($_GET['state_id']))
                                            ^
Top Expert 2006

Commented:
And also || instead of OR:

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

etc.
Top Expert 2006

Commented:
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();

Author

Commented:
oops school boy error

but it still doesnt seem to be working
Top Expert 2006

Commented:
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 ?

Author

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2006

Commented:
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 .  =)

Author

Commented:
ahh thankyou for that!

it seems php is a little more stricter than vB!

well done
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.