簡體   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