struppy
struppy

Reputation: 27

PHP MYSQL checkbox search

I am working on a checkbox search. I'm new to PHP. I would appreciate it if someone could help me. Basically the user checks the options required, hits submit and results matching the checkbox options is displayed. The checkboxes have a value of M for Male and F for Female which matches the data in the MYSQL table.

So if a user checks checkbox 'Male' (see code below) all data with 'Male' that have a value of 'M' in the MYSQL table are returned. And any checkboxes not checked should simply be ignored as the user is only interested in the option 'Male' being 'M' (true).

To sum up I only need the search to take into account checkboxes that have been checked and echo which users that has been selected(more like filtering) through PHP.Any help appreciated. Thanks

the table looks like this :

id      name     address    gender    race
-------------------------------------------

1. 1    Lee      NY          M        French
2. 2    James    LA          M        Colombian
3. 3    Kashi    JAPAN       F        Japanese

and i have a form with checkboxes like this :

<form action="shortlist.php" method="post">
 <label for="sel1">Gender:</label>
 Male:<input name="keyword[]" type="checkbox" value="M"  />
 Female:<input name="keyword[]" type="checkbox" value="F" />
 <button name = "myBtn" type="submit" class="btn btn-  default">Search</button>
</form>

SQL :

$sql="SELECT name, address,gender,race FROM talent1 WHERE gender = $gender'";

I'm suppose to echo something like this :

echo "<table width='100%'>\n";
//if (mysqli_num_rows($result) > 0){
//$row=mysqli_fetch_assoc($result);
//$muchneededid = $row["talent_id"];
     while ($row=mysqli_fetch_assoc($result))   
        echo'<tr>'."\n";
        echo '<tr>';
        echo '<th>Name</th>';
        echo '<th>Address</th>';
        echo '<th>Gender</th>';
        echo '</tr>';
        echo "<td>{$row["name"]}</td>\n" . "<td>{$row["address"]}</td>\n" . "<td>{$row["gender"]}</td>\n";  
        echo '</tr>'."\n";
        echo "</table>\n";
        }
        else
        {
       echo "0 results";
       }
       mysqli_close($conn);
       }

Upvotes: 1

Views: 464

Answers (1)

Jeff
Jeff

Reputation: 6953

given the information we've got it would be something like this:

<?php
//$_POST['keyword'] = array("M", "F");
$sql_addon = ''; // EDIT: this was one line below first, which of course created an 'undefined' error.
if(isset($_POST['keyword'])) {

    foreach($_POST['keyword'] as $k => $val) {
         $sql_addon.= " OR gender='$val'";
    }
}
$sql="SELECT name, address,gender,race FROM talent1 WHERE 1=1 ";
if($sql_addon) {
    $sql .= " AND (1=2 ".$sql_addon.")";
}
echo $sql;
// SELECT name, address,gender,race FROM talent1 WHERE 1=1 AND (1=2 OR gender=M OR gender=F)
?>

Those 1=1 and 1=2 might look silly, but it's (IMHO) the easiest way to generate that sql. 1=2 is there to create a falsy value, so that no records will be shown if none of the options are clicked.

Upvotes: 2

Related Questions