I’m creating a form that allows the user to search a database for different criteria: location, city, min price, max price, etc.
I have a “base” select statement: SELECT * FROM table.
In the search form there are several text boxes and dropdowns that may or may not have values, depending on what the user wants to search by.
I’m having trouble figuring out how to add WHERE clauses to the sql statement when there may or may not be a value to use for a particular table column (based on the user’s selection).
Thanks for anyone’s help!
In the code where you generate the SQL, check what fields the user has entered and append more checks to the query as you find more input from the user. Quick example in PHP:
I have intentionally left out sanitation / escaping of the post input to keep the example simple. But you obviously must sanitize the input as well.
Mind the extra space before the
WHEREotherwise the query will be broken.