這裏是修改後的代碼。仍然沒有運氣!請。幫助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>
你如何「運行窗體和PHP」? –
我會把它們放在兩個單獨的文件中,1.html用於表單和php的處理。 –
作爲一邊,你使用的MySQL擴展是舊的,不再推薦(http://ca2.php.net/manual/en/mysqlinfo.api.choosing.php)嘗試使用mysqli或pdo擴展。 – dnagirl