2012-12-01 62 views
0

我正在尝试用php和mysql创建一个简单的搜索脚本。我有选择的HTML标签,它是Php Mysql搜索问题

  1. 国家
  2. 区域
  3. 目的地

有了这个,我从从MySQL获得内容数据库。所以以下是我的PHP脚本。

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

好吧,如果我选择(其值是1,2,3等),它表示从数据库内容时,我选择国家它不会显示任何东西。我的查询有什么不对吗?

+0

以任何机会,你可以与我们分享您的MySQL表? – bonCodigo

回答

0

您的国家ELSEIF条件创造问题如果只是替换它,写if...elseif只有一个条件将得到执行。

使用此代码

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

谢谢@Pankaj Khairnar。它为我工作。 – Babu

1

isset($people)总为true;你需要检查,如果它不是empty还有:

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

你定义每个变量,因此所有变量总是“设置”。

if(isset($people))将始终运行,因为它的定义意味着isset($country)永远不会运行。

这需要更改为:

if(!empty($people)){ 

} 
if(!empty($country)){ 

} 
+0

我试图用这个.. – Babu

+0

它应该实现你所期待的 –

+0

我改变它。它显示第一个查询,但它不显示第二个查询..这是显示空的页面 – Babu