I have a form that collects search filters as text input and stores them in an array. I want to write a SQL query to select all rows except those where a particular field is equal to one of the users filters. I found documentation for excluding things from a query, but Im not sure how to do this if the user enters a lot of filters. Would I use a loop to create multiple queries for each array item? Can someone be able to point me in the direction to achieve this?
If I understand well what you want, you will have to use the array to construct the appropriate criteria.
Let’s seach that your Array is (value1, value2, value3). And the way I’m understanding your question, that means that those values need to be excluded.
Your basic query would be “SELECT field1, field2… FROM table1”. You would keep this basic query in a string variable.
So you would need to create the WHERE part " WHERE field1<>" value1 “AND field2<>” value2 " AND field3<>" value3
Be aware that because I don’t know which programming you’re using, I haven’t included any concatenation operators. Also that there is a white space before the word WHERE, and that I am assuming that all those values need to be exluded at the same time.
You definitely understand what Im trying to do, the only thing is im not going to know how many filters the user might enter. So I cant just write WHERE field1<>“value1” etc.I would need some sort of way to loop through the array of filters the user enters and build one query to unclude all of the filters in the WHERE section
I’m using PHP and AJAX. I’m messing around trying to send an array using POST through ajax but the for each loop in php is giving me an invalid argument error when trying to pass it the array. And yes all of the filters will apply to the same cilumn. I have a dB of recipes and a table of ingredients with a foreign key constraint to match the ids for each ingredient to the id of the corresponding recipe. So I want a join statement that selects all recipes except those where one of its ingredients matches one of the filters the user entered