0

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?

1
  • By any chance can you share your mysql tables with us? Commented Dec 1, 2012 at 15:01

3 Answers 3

1

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

if (isset($people) && !empty($people)) {
    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

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";
    }
}

Comments

0

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

}

6 Comments

it should achieve what your looking for
I change it. it's show first query but it's doesn't show second query.. It's show empty page
do you always need it to run people AND country? If so you need to remove the else in the elseif statement.
Well, I put elseif statement because sometime user can select only people or country or destination so if user select only 1 item then it's show something base on his selected item.
I removed else but doesn't show anything only first query is running.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.