lundi 28 septembre 2015

Perform MySql PHP search from a POST form with checkbox array

I have a POST form in which I have a checkbox with the name airport_use[].

<form id="stacks_in_p12751_n11834_page0_form" action="http://ift.tt/1P2KbCL" method="post">
<input type="checkbox" name="airport_use[]" value='Public'> 
<input type="checkbox" name="airport_use[]" value='Private'> 
<input type="checkbox" name="airport_use[]" value='Military'> 
<input type="checkbox" name="airport_use[]" value='Unknown'> 
<button class='ag_submit_button' type='submit'>Get Airport Information</button>
</form>

On the airport_search_results2.php results page I am trying to read in the variable for use in an SQL SELECT statement.

<?php 
$and_flag==0;
$sql = "SELECT * FROM table ";
if(isset($_POST['airport_use'])){
 {  if ($and_flag==0)
    {$clause = "WHERE "; // the first time the field exists use WHERE
    $and_flag=1;}   
    else{$clause = "OR ";} // use OR after the first field exists
        foreach($_POST['airport_use'] as $c){
            if(!empty($c)){
                 $sql .= $clause."airport_use LIKE '%{$c}%'";
                $clause = " OR ";//Change  to OR after 1st WHERE or AND
            }   
        }
    }
}
$sql .= "LIMIT 5000";

?> The problem I'm having is this code is only working if only one of the checkboxes is checked. More than one just makes the query hang. Any suggestions to fix this?

Thanks, Mike




Aucun commentaire:

Enregistrer un commentaire