Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7772297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:51:24+00:00 2026-06-01T16:51:24+00:00

I have a dynamic SQL search that im having a problem with on the

  • 0

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&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>";


}

?>

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:51:25+00:00Added an answer on June 1, 2026 at 4:51 pm

    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.

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

    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 $whereClause array – $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 –

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

    Output :
    INSERT INTOzoo('id','name','sound') VALUES (1,"Cow","Moo")

    Once you have verified that the query is correct you can remove the echo and the exit() commands.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dynamic list of items that will be used to POST information
I have an application that needs to return search results from a SQL Server
What is a good way to handle dynamic sql parameters? I have a search
I have a stored procedure that contains dynamic sql to return a result set.
I currently have a SQL query that returns results based on a dynamic number
I have a dynamic SQL statement I've created in a stored procedure. I need
I have a messy stored procedure which uses dynamic sql. I can debug it
I have some divs that have dynamic heights controlled by a 'click' function as
I have some divs that have dynamic heights controlled by a 'click' function as
I have a dynamic text file that picks content from a database according to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.