2012-12-01 67 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