【发布时间】:2014-06-06 21:35:28
【问题描述】:
我有一个汽车经销商网站。该网站从 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);
任何帮助将不胜感激。
【问题讨论】:
-
您检查了 $searchMake $searchBranch $minPrice 和 $maxPrice 是否真的是假的?
-
if (odbc_fetch_row($rs) === TRUE) { 我不是 100% 确定,但我认为您已经在此处获取第一个条目。所以第一个条目已经被提取,你总是少一个条目
-
您应该尝试删除该 if 语句并检查它是否有效!
-
存在 if 语句,因此当查询找不到某些内容时,您会收到消息说查询为空。
-
是的,但我认为已经获取了第一行但没有使用结果。尝试删除一次并检查它是否有效。