0

我有一个动态 SQL 搜索,我在“里程”列表选项上遇到问题:

    <?php
include('connection.php');



if(isset($_POST['Make']))
{
    $makeSearch = $_POST['Make'];
    $modelSearch = $_POST['Model'];
    $colourSearch  = $_POST['Colour'];
    $townSearch = $_POST['Town'];
    $regionSearch = $_POST['Region'];
    $regiSearch = $_POST['Reg'];
    $maxSearch = $_POST['MaxPrice'];
    $minSearch = $_POST['MinPrice'];
    $maxMilSearch = $_POST['MaxMil'];

}
else
{

    $makeSearch = $_GET['Make'];
    $modelSearch = $_GET['Model'];
    $colourSearch  = $_GET['Colour'];
    $townSearch = $_GET['Town'];
    $regionSearch = $_GET['Region'];
    $regiSearch = $_GET['Reg'];
    $maxSearch = $_GET['MaxPrice'];
    $minSearch = $_GET['MinPrice'];
    $maxMilSearch = $_GET['MaxMil'];


}




if(!isset($_GET['pageNo']))
{
    $pageNo = 0;
}


else
{
    $pageNo = $_GET['pageNo'];
}

$noOfRecords = 4;
$startRecord = $pageNo * $noOfRecords;
$nextPage = $pageNo + 1;
$prevPage = $pageNo - 1;
$curpage = $pageNo + 1;




        $sql2 = "SELECT * FROM cars WHERE 1=1";
    if($makeSearch !== "Select Make")
    {
        $sql2 .= " AND make = '$makeSearch'";
    }
    .....................
    if($maxMilSearch !== "Select Max Mileage")
    {
        $sql2 .= " AND miles <= '$maxMilSearch'";
    }

















        $sql = "SELECT * FROM cars WHERE 1=1";
    if($makeSearch !== "Select Make")
    {
        $sql .= " AND make = '$makeSearch'";
    }
    .........................
    {
        $sql .= " AND miles <= '$maxMilSearch'";
    }

    $sql .= "LIMIT $startRecord, $noOfRecords";


    $result2 = mysql_query($sql2,$odbc) or die ("Can't run query1");
    $result = mysql_query($sql,$odbc) or die ("Can't run query2");

    echo"$sql";

    $num_rows = mysql_num_rows($result2);


echo"$num_rows";

$last = ceil($num_rows/$noOfRecords);


if(mysql_num_rows($result) == 0)
{
    echo "No cars found";

}



else

{
    echo "<table border = 1>";
    while($row=mysql_fetch_object($result))
    {
        echo "<tr>";
            echo"<td>";
                echo$row->make;
            echo"</td>";
            echo"<td>";
                echo$row->model;
            echo"</td>";
            echo"<td>";
                echo$row->Reg;
            echo"</td>";
            echo"<td>";
                echo$row->colour;
            echo"</td>";
            echo"<td>";
                echo$row->miles;
            echo"</td>";
            echo"<td>";
                echo$row->price;
            echo"</td>";
            echo"<td>";
                echo$row->dealer;
            echo"</td>";
            echo"<td>";
                echo$row->telephone;
            echo"</td>";
            echo"<td>";
                echo$row->description;
            echo"</td>";
            echo"<td>";
                echo$row->carIndex;
            echo"</td>";
            echo"<td>";
                echo$row->region;
            echo"</td>";
            echo"<td>";
                echo$row->town;
            echo"</td>";
            echo"<td><a href='look.php?carIndex=$row->carIndex&Reg=$row->Reg&make=$row->make&model=$row->model&colour=$row->colour&price=$row->price&miles=$row->miles&dealer=$row->dealer&telephone=$row->telephone&description=$row->description&region=$row->region'>More Info</a></td>";





    echo"</tr>";
    }
    echo "<tr>";
echo"$curpage";
echo"$last";
if($pageNo >= 1)
{

        echo "<td><a href='search.php?pageNo=$prevPage&Make=$makeSearch&Model=$modelSearch&Reg=$regiSearch&MaxMil=$maxMilSearch&Colour=$colourSearch&Town=$townSearch&Region=$regionSearch&MaxPrice=$maxSearch&MinPrice=$minSearch'>Previous</a></td>";
}

if($pageNo < $last - 1)
{
        echo "<td><a href='search.php?pageNo=$nextPage&Make=$makeSearch&Model=$modelSearch&Reg=$regiSearch&MaxMil=$maxMilSearch&Colour=$colourSearch&Town=$townSearch&Region=$regionSearch&MaxPrice=$maxSearch&MinPrice=$minSearch'>Next</a></td>";
}       
    echo"</tr>";
    echo"</table>";


}

?>

据我所知,我遇到的问题是“里程”输入被忽略了。如果我输入(从我的下拉列表中)例如 5000 英里以返回所有低于此值的汽车,它会返回任何里程的随机结果。我要说的是,如果我选择例如汽车制造商然后选择里程,它将返回正确的结果。

真的不确定我在这里做错了什么,因为我的所有其他选项都可以正常工作。

任何帮助都会很棒!

4

1 回答 1

3

如果一切正常,回显最终生成的查询将为您提供提示。通过 PHPMYADMIN 运行它还可以很好地表明您的查询是有效的并返回正确的结果。

这种组装查询的略有不同的方法可能会对您有所帮助。它还使 IMO 更易于阅读。

$sql = "SELECT * FROM cars WHERE ";
$whereClause = array();

if($makeSearch !== "Select Make"){
    $whereClause[] = "make = '$makeSearch'";
}

...

if($maxMilSearch !== "Select Max Mileage"){
    $whereClause[] = "miles <= '$maxMilSearch'";
}

$sql .= implode(" AND ",$whereClause) . "LIMIT $startRecord, $noOfRecords";

echo $sql;
exit();

那应该将最终查询打印到屏幕上并退出。

我正在使用 的简写版本array_push()将元素添加到$whereClause数组 - $whereClause[] = VALUE。这[]就像一个推动动作;在数组末尾添加一个附加元素。

我使用的另一个有用的函数是implode()函数。此方法采用一个数组并使用给定的字符串将它们连接成一个字符串以连接它们。像您在这里所做的那样组装查询时,它非常有用。

除了将它用于 where 子句之外,您还可以将它用于值列表 -

$cowDetails = array(
 '1',
 '"Cow"',
 '"Moo"'
);
$insertSql = "INSERT INTO `zoo` ('id','name','sound')  
              VALUES  (".implode(',',$cowDetails).")";

echo $insertSql;
exit();

输出
INSERT INTO动物园('id','name','sound') VALUES (1,"Cow","Moo")

验证查询正确后,您可以删除echoexit()命令。

于 2012-04-07T11:34:49.833 回答