【问题标题】:PHP Search Form doesn't return all the resultsPHP 搜索表单不返回所有结果
【发布时间】: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 语句,因此当查询找不到某些内容时,您会收到消息说查询为空。
  • 是的,但我认为已经获取了第一行但没有使用结果。尝试删除一次并检查它是否有效。

标签: php odbc


【解决方案1】:

删除这一行:

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

您已经在此处获取第一个条目,但您没有使用结果..

用它来检查它是否为空:

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

【讨论】:

  • 看,这行得通,但现在 else 语句没有执行。我的意思是,如果找不到所需车辆的消息不会显示。请查看我的更新代码...
  • @deon4110 啊,也许你只需要检查真假,检查我更新的答案。我删除了!== 0。因为 odbc_num_rows 可以返回 0、false 或 -1。这将全部评估为 false,所以现在应该可以工作了!
  • 它对我有用...尝试检查“-1”,例如 if(xxx == -1) {...}
  • 是的,它可以显示消息。现在它所做的只是显示消息,即使车辆可用。
  • 你能 var_dump($rs);并检查当你有 0 个结果时你得到了什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-26
  • 2019-12-05
  • 2020-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多