我有一个汽车经销商网站。该网站从Access数据库(.mdb)中提取车辆信息。我无法改变这一点,因为集成来自他们当前的DMS,它将数据保存到.mdb。
在这个网站上,我有一个搜索表单来查询特定车辆的数据库。表单有效,查询也是如此。
但是,这里有问题;例如,数据库中有5辆福特汽车,并且用户搜索所有可用的福特,查询只返回可用的5辆中的4辆。
请参阅下面的代码。
$conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", '', '');
$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 || $searchBranch || $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 ($searchBranch) {
$sql .="{$combine}Branch LIKE '%$searchBranch%' ";
}
$rs = odbc_exec($conn, $sql);
$rs = odbc_exec($conn, $sql);
if (odbc_num_rows( $rs ) == -1) {
echo "We don’t have the vehicle you are looking for right now, but send us your vehicle requirements and we will be sure to find you one!";
} else {
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><a href=/selected-vehicles?Id=$id>" . $make . "</td><td><a href=/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);
非常感谢任何帮助。
答案 0 :(得分:1)
删除此行:
if (odbc_fetch_row($rs) === TRUE) {
您已经在此处获取第一个条目,但您没有使用结果..
用它来检查它是否为空:
if(odbc_num_rows( $rs ) == 0){
// when no result
} else {
// when result
}