Skip to content
Advertisement

Filter data from database with multiple user selections

Currently I’m developing a search form so my SQL query needs to change with user input. Please see the below code sample.

In order to execute this statement the user must select all the options; the statement will not work if the user selects one or two options.

Advertisement

Answer

Start out with a placeholder like 1=1 which will always be true, and then use AND as a prefix instead of a suffix.


But as pointed out in the other answer you need to use prepared statements. So, assuming you’re using mysqli, which everyone seems to do for some reason:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement