我正在开发一个用户可以搜索数据库的项目。搜索字段都是选择框。你可以搜索;品牌和/或制造和/或分支和/或年份和/或最低价格和最高价格。
请在下面找到我的代码:
<?php
$dbName = "myURL/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 ";
if ($searchMake || $searchModel || $searchBranch || $searchYear || $minPrice || $maxPrice) {
$sql .= "WHERE ";
}
$combine = '';
if ($minPrice) {
$sql .="{$combine}Price BETWEEN %$minPrice% "; $combine = 'BETWEEN ';
}
if ($maxPrice) {
$sql .="AND %$maxPrice% "; $combine = 'AND ';
}
if ($searchMake) {
$sql .="{$combine}Make LIKE '%$searchMake%' "; $combine = 'AND ';
}
if ($searchModel) {
$sql .="{$combine}Model LIKE '%$searchModel%' "; $combine = 'AND ';
}
if ($searchBranch) {
$sql .="{$combine}Branch LIKE '%$searchBranch%' "; $combine = 'AND ';
}
if ($searchYear) {
$sql .="{$combine}Year LIKE '%$searchYear%' "; $combine = 'AND ';
}
//$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);
echo "\n\n$sql\n\n";
}
//} else {
//$sql = "SELECT Id, Make, Model, Year, Price, SpecialPrice, Branch, StockNO FROM Vehicle ORDER BY Make";
//$rs = odbc_exec($conn, $sql);
//echo "Your search criteria did not match anything in our database.";
//}
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";
}
//echo "\n\n$sql\n\n";
//echo $minPrice;
//echo $maxPrice;
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!");
}
?>
上述代码的目的是让用户搜索数据库。但从逻辑上讲,当您搜索时,通常不会选择所有可用字段。对于Brand,Make,Branch和Year,如果您只选择一些搜索,则搜索工作正常,当您选择全部搜索时,搜索也可以。但只要您选择最低和最高价格,代码就会停止执行。
我使用此打印查询来查看结构:echo "\n\n$sql\n\n";
,我似乎无法找到问题。没有显示错误,它只是不起作用。如果您选择最低和最高价格,这就是显示的内容:SELECT Id, Make, Model, Year, Price, SpecialPrice, Branch, StockNO FROM Vehicle WHERE Price BETWEEN %100000.0% AND %150000.0% AND Make LIKE '%HYUNDAI%' AND Model LIKE '%I20%' AND Branch LIKE '%EAST_RAND%' AND Year LIKE '%2011%' There is an error in the SQL!
此外,在上面的代码中,我可以添加一条消息,通知用户在数据库中找不到所选项目吗?
非常感谢任何帮助。