2012-10-22 107 views
0

這裏是修改後的代碼。仍然沒有運氣!請。幫助PHP搜索表單過濾器使用多個下拉菜單

<html> 
<body> 

<?php 

$mysqli = new mysqli("localhost", "root", "password", "test"); 

$whereClauses = ''; 
$numLocations = count($_POST['Locations']); 
$numJobs = count($_POST['Jobs']); 
$i = 0; 
if (! empty($_POST['Locations'])) { 
    foreach ($_POST['locations'] as $location) { 
    $whereClauses .="Locations='".mysql_real_escape_string($location)."'"; 
    if ($i++ == $numLocations) { 
    $whereClauses .= " AND"; 
    } 
    } 
    } 
    if (! empty($_POST['Jobs'])) { 
    foreach ($_POST['Jobs'] as $job) { 
    $whereClauses .="Jobs='".mysql_real_escape_string($job)."'"; 
    } 
    if ($i++ == $numJobs) { 
    $whereClauses .= " AND"; 
    } 
    } 
    $sql = "SELECT * FROM mytable '".$whereClauses."' ORDER BY id DESC '".$limit."'"; 

$result=mysql_query($sql); 

while ($row = mysql_fetch_array($result)) { 
echo $row['Locations']; 
echo $row['Jobs']; 
} 

?> 
</body> 
</html> 

========================================== ===== 我創建了一個HTML和PHP文件,用於根據多個下拉篩選器篩選Web表單數據。這裏是形式。當我運行表單和PHP時,瀏覽器中沒有看到任何結果。也沒有錯誤。我正在研究另一個論壇成員發佈的示例。 請幫忙。提前致謝。

<form action="showJobs_new.php" method="post"> 
<select name="Locations"> 
<option value="" selected="selected">All Locations</option> 
<option value="arizona">Arizona</option> 
<option value="alaska">Alaska</option> 
</select> 
<select name="Jobs"> 
<option value="" selected="selected">All jobs</option> 
<option value="Carpenter">Carpenters</option> 
<option value="Plumbers">Plumbers</option> 
</select> 
<input type="submit" value="search jobs" /> 
</form> 

showJobs_new.php: 

<html> 
<body> 

<?php 

$username="root"; 
$password="password"; 
$database="test"; 

mysql_connect(localhost,$username,$password); 
@mysql_select_db($database) or die("Unable to select database"); 

$whereClauses = array(); 
if (! empty($_POST['Locations'])) $whereClauses[] ="Locations='".mysql_real_escape_string($_POST['Locations'])."'"; 
if (! empty($_POST['Jobs'])) $whereClauses[] ="Jobs='".mysql_real_escape_string($_POST['Jobs'])."'"; 
$where = ''; 
if (count($whereClauses) > 0) { $where = ' WHERE '.implode(' AND ',$whereClauses); } 

$sql = mysql_query("SELECT * FROM mytable ORDER BY id DESC $limit" .$where); 

$result=mysql_query($sql); 
or die("Error: ".mysql_error()."<br />Query: ".$sql); 

while ($row = mysql_fetch_assoc($result)) { 
echo $row['Locations']; 
echo $row['Jobs']; 
} 

?> 
</body> 
</html> 
+0

你如何「運行窗體和PHP」? –

+0

我會把它們放在兩個單獨的文件中,1.html用於表單和php的處理。 –

+0

作爲一邊,你使用的MySQL擴展是舊的,不再推薦(http://ca2.php.net/manual/en/mysqlinfo.api.choosing.php)嘗試使用mysqli或pdo擴展。 – dnagirl

回答

3

更改查詢

$sql = mysql_query("SELECT * FROM mytable " .$where." ORDER BY id DESC $limit"); 

查詢的順序似乎是錯誤的。

+0

@Awais Qarni:感謝編輯朋友。 – Sutha

+0

感謝雙方的幫助。我糾正了那部分。仍然沒有顯示,也沒有錯誤:( –

+0

)您在代碼中使用了兩次mysql_query()函數,請刪除第二個mysql_query()函數 $ sql = mysql_query(「SELECT * FROM mytable ORDER BY id DESC $ limit」 $ where); $ result = mysql_query($ sql); //刪除這行......並使用mysql_fetch_assoc()函數中的$ sql – Sutha

1

嗨親愛你在查詢中有兩個錯誤。

1 - 這是因爲您正在輸入錯誤的子句順序。您需要了解查詢中子句的優先級。當您運行查詢,條款順序應該像

  • 選擇

  • 集團通過

  • ORDER BY

  • 使用的mysql_query兩次被user1599669

    的建議限制

2 - 所以,你的查詢應該看起來像

$sql = mysql_query("SELECT * FROM mytable $where ORDER BY id DESC $limit"); 

更多參考從mysql dev讀取。