繁体   English   中英

PHP搜索表单不会返回所有结果

[英]PHP Search Form doesn't return all the results

我有一个汽车经销商网站。 该网站从Access数据库(.mdb)提取车辆信息。 我无法更改此设置,因为集成来自其当前的DMS,该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);

任何帮助将不胜感激。

删除此行:

  if (odbc_fetch_row($rs) === TRUE) {

您已经在这里获取第一个条目,但没有使用结果。

使用它来检查其是否为空:

if(odbc_num_rows( $rs ) == 0){
        // when no result
} else {
        // when result
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM