PHP combo boxes + MySQL query

Soldato
Joined
6 Jun 2011
Posts
2,741
Hey guys,

So I am currently trying to construct a mysql query that searches through a database.

The problem I have is that I am trying to use multiple checkbox values to construct the WHERE query.

Anybody know a good way of doing this?

Thanks :)
 
Without knowing the significance of the checkboxes it would be a bit hard. Are you using them to select data from different columns/tables, or is it to select from a range of items that are to be found all in a single column?

If the latter, then each checkbox has an ID, this will come into the $_POST[] key/value hash that you can see if it has been checked via isset(), and if so, put the relevant criteria into a comma delimited string.

You'd then include that into your query something like: "... WHERE thiscolumn in(" . $yourCommaDelimitedValues . ")".

(Whilst validating the inputs are valid of course and not tricking you with injected SQL or the like).

Might be able to suggest something better if there are more details to share though.

Thanks for the response. Silly me, I meant combo boxes! But I know they have a similar principle.

The thing is I am going to have multiple combo boxes.

So if I run something like:


The first line does what you said but also checks to see if the combo box is set to 'all' for instance. Then if it is true it will add the current value into a variable which can then be used in an SQL query. The problem is I have multiple boxes which need to be used in the same query.

Hopefully this makes sense!

Thanks :)
 
Last edited:
Thanks for the response.

The problem is that not all of the combo boxes have to be selected. The default option for all the combo boxes basically have a value which is "".

So then any or all of the combo boxes can be set to other than "" and each one corresponds to a column in the table.

Constructing the SQL select query would be something like that I guess. But what happens when the variables for a column box are set to default?

Cheers :)
 
Thanks very much for your response. Do you mind explaning it a bit more? I am a beginner really and don't really understand how the bit in brackets works!

Thanks a lot :)
 
Last edited:
Thanks very much for that. That is very kind!

The only thing is I am not sure if this will work in my instance?

If I am adding the values into variables on submit like this:

That is just for one combo box. So if the value is equal to ''. Then searching this in the database, using the filter method you gave will not work will it?

Or am I looking at this wrong?

Thanks :)
 
Last edited:
Yes that's what I had actually just got to. Thanks so much, that is a great help.

One last thing that I do not understand. Is because it is in an SQL query why does the '$choice' = '' not cause a problem for instance?

Is it not looking in the table somewhere for this? I understand that when that is true it will not look at the other condition in the brackets.

Hopefully that makes sense!

Thanks :)
 
Thanks for the response,

Would that work in this case though? Using the filter in the where statement allows it to skip the column search, if the variable is empty. Would this be possible using the other way that you have posted?

Thanks again :)
 
Back
Top Bottom