jungle_programmer
jungle_programmer

Reputation: 11

MySQL query using multiple criteria from checkboxes

I would like to do a multiple search query usig multiple checkboxes which represent particular textboxes.

How do i create a mysql query which will be filtering the checked and unchecked checkboxes (probably using if statements)?
The query should be able to filter the checked and ucnchecked boxes and query them using the AND condition.

Thanks

Upvotes: 0

Views: 2510

Answers (1)

Jage
Jage

Reputation: 8086

Mysql won't be able to handle your html form post. You will need to use a server-side language to check for those checkboxes being set and then modify your query with additional conditions.

You would likely get a better response if you were more specific, but I'll throw you an example with php. Assuming your form looks something like:

<form method="post">
    <input type="checkbox" name="field1_cb" value="1" />
    <input type="text" name="field1_txt" />
    <br /><br />
    <input type="checkbox" name="field2_cb" value="1" />
    <input type="text" name="field2_txt" />
</form>

Your server code might look something like:

$query = "SELECT * FROM tbl WHERE field0 = 'asdf' ";
$fields = array('field1','field2');
foreach($fields as $f){
    if(isset($_POST[$f.'_cb'])){
        $query .= " AND $f = ".$_POST[$f.'_txt'];
    }
}
//Run your query... 

Please don't copy and paste this code directly, it is disgustingly insecure. Just trying to get you started.

Upvotes: 1

Related Questions