简体   繁体   中英

PHP Search Form doesn't return all the results

I have a vehicle dealership website. This website draws the vehicle information from an Access database (.mdb). I can't change this as the integration is coming from their current DMS which saves the data to an .mdb.

On this website, I have a search form to query the database for specific vehicles. The form works, and so does the query.

But, here's problem; say for instance there are 5 Ford cars in the database, and the user searches for all the available Fords, the query only returns 4 of the available 5.

Please see my code below.

        $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);

Any help would be greatly appreciated.

Delete this line:

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

You are already fetching the first entry here but you are NOT using the result..

Use this to check if its empty or not:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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