Babu
Babu

Reputation: 7

Php Mysql Search Issue

I'm trying create a simple search script with php and mysql. I've html select tag which is

  1. people
  2. country
  3. region
  4. destination
  5. from
  6. to

With this I get the content from from mysql database. so following is my php script.

if(isset($_GET['Submit']) && $_GET['Submit'] == "Search")
{
$people = mysql_real_escape_string(htmlspecialchars(trim($_GET['people'])));
$country =  mysql_real_escape_string(htmlspecialchars(trim($_GET['country'])));
$region =  mysql_real_escape_string(htmlspecialchars(trim($_GET['region-depart'])));
$destination = mysql_real_escape_string(htmlspecialchars(trim($_GET['destination'])));
$from = mysql_real_escape_string(htmlspecialchars(trim($_GET['from'])));
$to = mysql_real_escape_string(htmlspecialchars(trim($_GET['to'])));

if(isset($people))
{

$search = mysql_query("SELECT * FROM property_step1 WHERE pro_no_sleep LIKE 
'%$people%'");
$num = mysql_num_rows($search);

while($result = mysql_fetch_array($search))
    {
        $propertyid = (int) $result['propertyid'];          
        echo $country_d = $result['pro_country'];
        echo $region_d = $result['pro_state'];
        echo $destination_d = $result['pro_city'];

    }
}

elseif(isset($country))
{
$search2 = mysql_query("SELECT * FROM property_step1 WHERE pro_country LIKE 
'%$country%'");
$num = mysql_num_rows($search2);        

while($result2 = mysql_fetch_array($search2))
    {
        $propertyid = (int) $result2['propertyid'];         
        echo $country_d = $result2['pro_country'];
        echo $region_d = $result2['pro_state'];
        echo $destination_d = $result2['pro_city'];

    }
}
else
{
    echo "nope";
}       
}

Well, if i select people (which value is 1, 2, 3 and so on) it's show the content from database but when i select country it's doesn't show anything. Is there anything wrong in my query?

Upvotes: 0

Views: 108

Answers (3)

Samuel Cook
Samuel Cook

Reputation: 16828

You are defining each variable so all variables will always "be set".

if(isset($people)) will always run, as it is defined meaning that isset($country) will never run.

This needs to be changed to:

if(!empty($people)){

}
if(!empty($country)){

}

Upvotes: 0

Isaac
Isaac

Reputation: 2721

isset($people) always evaluates to true; you need to check if it is not empty as well:

if (isset($people) && !empty($people)) {
    // ...
}

Upvotes: 1

Pankaj Khairnar
Pankaj Khairnar

Reputation: 3108

Your elseif condition for country is creating problem replace it with if only, writing if...elseif only one condition will get execute.

use this code

if (isset($_GET['Submit']) && $_GET['Submit'] == "Search") {
    $people = mysql_real_escape_string(htmlspecialchars(trim($_GET['people'])));
    $country = mysql_real_escape_string(htmlspecialchars(trim($_GET['country'])));
    $region = mysql_real_escape_string(htmlspecialchars(trim($_GET['region-depart'])));
    $destination = mysql_real_escape_string(htmlspecialchars(trim($_GET['destination'])));
    $from = mysql_real_escape_string(htmlspecialchars(trim($_GET['from'])));
    $to = mysql_real_escape_string(htmlspecialchars(trim($_GET['to'])));

    if (isset($people)) {
        $search = mysql_query("SELECT * FROM property_step1 WHERE pro_no_sleep LIKE 
'%$people%'");
        $num = mysql_num_rows($search);

        while ($result = mysql_fetch_array($search)) {
            $propertyid = (int) $result['propertyid'];
            echo $country_d = $result['pro_country'];
            echo $region_d = $result['pro_state'];
            echo $destination_d = $result['pro_city'];
        }
    } 
    if (isset($country)) {
        $search2 = mysql_query("SELECT * FROM property_step1 WHERE pro_country LIKE 
'%$country%'");
        $num = mysql_num_rows($search2);

        while ($result2 = mysql_fetch_array($search2)) {
            $propertyid = (int) $result2['propertyid'];
            echo $country_d = $result2['pro_country'];
            echo $region_d = $result2['pro_state'];
            echo $destination_d = $result2['pro_city'];
        }
    } else {
        echo "nope";
    }
}

Upvotes: 0

Related Questions