I have a dynamic SQL search that im having a problem with on the ‘mileage’ down list option:
<?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®ion=$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>";
}
?>
The problem I’m having is the ‘Mileage’ input is as far as I can see, being ignored. If I enter (from my drop down list) for example 5000 miles to return all cars below and equal to this, its returning random results with any mileage. What I will say is that if I select for example a car make then select a mileage it will return the correct result.
Really not sure what im doing wrong here as all my other options work correctly.
Any help would be great!
Echoing out the final resulting query will give you a hint if everything is in order. Running it through PHPMYADMIN would also give you a good indication that your query is valid and returns the correct results.
This slightly different method of assembling the query might help you. It also IMO makes it a little easier to read.
That should print out the final query to the screen and thex exit.
I’m using the shorthand version of
array_push()to add elements to the$whereClausearray –$whereClause[] = VALUE. The[]is like a push action; Adding an additional element to the end of the array.The other useful function I’ve used is the
implode()function. This method takes an array and joins it together into one string using the given string to connect them. It is really useful when assembling queries as you are doing here.In addition to using it for the where clause you could use it for a list of values too –
Once you have verified that the query is correct you can remove the
echoand theexit()commands.