mysql PHP query question

Posted by camran on Stack Overflow See other posts from Stack Overflow or by camran
Published on 2009-10-13T15:52:03Z Indexed on 2010/04/11 11:23 UTC
Read the original article Hit count: 156

Filed under:
|

Ok, i have a problem here...

I am sending values of drop down lists via ajax to this PHP file.

Now I want to search a mysql database using these values, which I have managed to do, BUT, only if I set the values to something...

Take a look:

     $query = "SELECT * FROM cars_db WHERE price BETWEEN '$cars_price_from' AND '$cars_price_to' AND year BETWEEN '$cars_year_from' AND '$cars_year_to' AND mileage BETWEEN '$cars_mileage_from' AND '$cars_mileage_to' AND gearbox = '$cars_gearbox' AND fuel = '$cars_fuel'";

now, what if the user doesnt select any "price_from" or "year_from"... The fields are only optional, so if the user doesnt enter any "price from" or "year from", then the user wants ALL cars to show...

Do I have to write a query statement for each case or is there another way?

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql