我在一个汽车经销商网站上工作。该经销商使用第三方桌面应用程序来管理库存和在售车辆。此第三方应用程序将车辆数据保存到访问数据库 (.mdb)。我成功连接到这个数据库,以及查看个人记录等等。到目前为止一切都很好。
难题的最后一块是高级搜索条件。这个搜索条件有六个输入;品牌、型号(品牌和型号输入是文本,它们也是使用 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!");
}
?>
我将如何使消息出现?
编辑:查询现在可以工作,它会根据所做的选择显示被删除的结果,但必须进行所有选择,否则它不会显示任何内容。
请记住,必须选择品牌。不可能是所有品牌。否则,您可以查看整个表。
如何构建上述查询以将“所有型号”和“任何分支”以及“任何年份”和“任何价格”作为有效选择?