I have a form which is used to search the database. The form consists of four fields, including two date fields: (Col1, Col2, Datefm, DateTo). Users have the option to fill the values in these fields. They may fill the value in one field or two field or three field or all fields. Based on these values my query has to search the database.
I made a query which fetches the data, but it works only when all the values in the fields are available. I want it to exclude the where conditions which are not filled by the user. How can i achieve that?
I tried the following code.
SELECT *
FROM Table1
WHERE tcol1=$col1
AND tcol2=$col2
AND tDateto=$Dateto
AND tdatefm=$Datefm
2
Answers
You can try using this OR function. like the one below
As ysth has pointed out in the comments above, you should build your WHERE clause dynamically based on which form fields are populated.
Here’s a very simple example which will work for the presented use case (mysqli based as your question was originally tagged with mysqli):