我正在汽車經銷商網站上工作。該經銷商使用第三方桌面應用程序來管理庫存和出售的車輛。此第三方應用程序將車輛數據保存到Access數據庫(.mdb)。我成功連接到這個數據庫,以及查看單個記錄等等。所有這一切都很好。PHP和MySql中的多重搜索條件
拼圖的最後一部分是高級搜索條件。這個搜索標準有六個輸入;品牌,型號(品牌和型號輸入是文本,它們也是使用AJAX的級聯下拉),分支,年份(文本),最低價格和最高價格(兩個價格輸入是數據庫中的數字數據類型)。
用戶將很可能不會使用所有可用的輸入,因爲更多的往往不是,那會給他們沒有結果。
而且,如果以後所有的搜索工作,並有人搜索的東西,但那個東西是不是在數據庫中可用,消息已經出現,告知所請求的車輛目前無法使用用戶。我會如何去做這樣的事情?
這是我的代碼到目前爲止。
<?php
$dbName = "F:/Domains/autodeal/autodeal.co.za/wwwroot/newsite/db/savvyautoweb.mdb";
// Throws an error if the database cannot be found
if (!file_exists($dbName)) {
die("Could not find database file.");
}
// Connects to the database
// Assumes there is no username or password
$conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", '', '');
if (isset($_REQUEST['submit'])) {
$searchMake = addslashes($_POST['makeSelection']);
$searchModel = addslashes($_POST['modelSelection']);
$searchBranch = addslashes($_POST['branchSelection']);
$searchYear = addslashes($_POST['yearSelection']);
$minPrice = addslashes($_POST['minPriceSelection']);
$maxPrice = addslashes($_POST['maxPriceSelection']);
$sql = "SELECT Id, Make, Model, Year, Price, SpecialPrice, Branch, StockNO FROM Vehicle WHERE Price BETWEEN $minPrice AND $maxPrice AND Make LIKE '$searchMake' AND Model LIKE '$searchModel' AND Year LIKE '$searchYear' AND Branch LIKE '$searchBranch'";
$rs = odbc_exec($conn, $sql);
//} else {
//$sql = "SELECT Id, Make, Model, Year, Price, SpecialPrice, Branch, StockNO FROM Vehicle ORDER BY Make";
//$rs = odbc_exec($conn, $sql) or die (odbc_errormsg());
}
echo "\t" . "<tr>\n";
echo "\t" . "<th>Make</th><th>Model</th><th>Year</th><th>Price</th><th>Special Price</th><th>Location</th><th>Stock Number</th>" . "\n";
while (odbc_fetch_row($rs)) {
$id = odbc_result($rs, Id);
$make = odbc_result($rs, Make);
$model = odbc_result($rs, Model);
$year = odbc_result($rs, Year);
$price = odbc_result($rs, Price);
$specialPrice = odbc_result($rs, SpecialPrice);
$branch = odbc_result($rs, Branch);
$stockNo = odbc_result($rs, StockNO);
echo "\t" . "<tr>\n";
echo "\t\t" . "<td>" . $make . "</td><td><a href=/newsite/selected-vehicles?Id=$id>" . $model . "</a></td><td>" . $year . "</td><td>" . $price . "</td><td>" . $specialPrice . "</td><td>" . $branch . "</td><td>" . $stockNo . "</td>\n";
echo "\t" . "</tr>\n";
}
odbc_free_result($rs);
odbc_close($conn);
// This message is displayed if the query has an error in it
if (!$rs) {
exit("There is an error in the SQL!");
}
?>
我該如何讓消息出現?
編輯:查詢工作,現在,它顯示根據所做的選擇的derisred結果,但所有的選擇都必須作出否則不顯示任何內容。
請注意品牌必須被選中。它不可能是所有品牌。否則,您可以查看整個表格。
我怎樣才能構建上面的查詢有一個「全模式」和「任何一間分行」和「任何一年」和「任何代價」作爲有效的選擇?
addslashes?!?請聘請一個人。 –
如果你打印出這條sql語句,它說什麼? 'echo「\ n \ n $ sql \ n \ n」; '。你可以添加到你的問題? – bobkingof12vs
@ bobkingof12vs我編輯了這個問題。 – Deon