Reputation: 7
I'm trying create a simple search script with php and mysql. I've html select tag which is
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
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
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
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