Your Open Source

 
Related Posts
QA  >>  Web Development  >>  PHP

Search data using more than one search fields

Search data using more than one search fields :-

I have a php search page which is having more than one search felds. This is my query
SELECT * FROM vehicles WHERE VehicleType='$type' AND VehicleMake='$make' AND VehicleModel='$model' AND Year= '$year' AND Price BETWEEN '$minPrice' AND '$maxPrice'

This will get results only if all fields match. But I want to get results if only one field match. How can I do that?

ex: If user enter only vehicle make and price I want to display data by searching db only the entered fields.

  • hits 124
  • datetime Oct 06 09 07:12:21
  • author Tech
  • rating

Rating : 12345
Tags :- PHP

Answers

sekar
Oct 08 09 03:51:09
well.. you need to check the each variable is value there, if value is not null then you need to put the condition in the SQL query statment, for example code as follow,

if($type !='')
       $cond .= "AND VehicleType='$type'";
if($make!='')

       $cond .= "AND VehicleMake='$type'";


$query = "SELECT * FROM vehicles WHERE 0=0 $cond";

you check with the code and let me know.

Your Name:
Your Answers :
<> is a code tag
Preview
Enter the verification code: