• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1173
  • Last Modified:

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
0
s34n
Asked:
s34n
  • 5
  • 3
1 Solution
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now