View Full Version : What is wrong with my query
11-21-2010, 10:04 PM
I am building a gui for a fictitious car company where users can look at cars. I want them to be able to only fill in one text box, and all the cars that meet that requirement will be shown to them. However, with the query I have now, the user can't just put in a model and they can't just put in a make. They have to put in both, or no results will show up. I know that I can replace the AND with an OR, but then when the user chooses a model, all the cars that are made by the same make as their chosen car show up. I really have no clue how to fix this.
$Query=("SELECT * FROM Inventory WHERE Make = '$Make' AND Model = '$Model' OR Year = '$Year' OR Price = '$Price' OR Mileage = '$Mileage' OR Color = '$Color'") or die (mysql_error());
11-21-2010, 10:51 PM
If you make the AND an OR, and they enter a make, like "Focus", they should
only see the one's with "Focus", not all of the Ford cars. So, I wonder if your
database somehow has the make and model in the same column?
11-21-2010, 10:58 PM
I made a mistake with what I said the error was. I change the AND to OR and everything works, except when i try to put in a make and model at the same time. For example, if i put in Honda Accord, I will get all of the Honda's in my database. Not just the honda accords.
11-22-2010, 12:34 AM
Yes, I would expect you would, because it matches one or the other.
You column called "Make" will match Honda. I suppose you could make
it match "exact". But if they entered "Honda Accord", they would get
You could do two separate queries. If their searchword matches something
from more than one column, determine which column to use ... but the problem
there is, which one? Maybe if their searchword matches more than one column,
tell them that they need to be more specific?
"You matched both "Honda" and "Accord", which one would you like to use?"
( ) Honda
( ) Accord
( ) Both
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.