Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What is wrong with my query

    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.
    PHP Code:
    $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()); 

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,464
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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?

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,464
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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
    zero matches.

    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •