开发者

MySQL query using multiple criteria from checkboxes

开发者 https://www.devze.com 2022-12-27 16:31 出处:网络
I would like to do a multiple search query usig multiple checkboxes which represent particular textboxes.

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.开发者_如何学C

Thanks


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号