1

I'm trying to filter the search results of my MySQL database, I am wanting to filter the results by selecting values from two drop down boxes, the first box being Club and the second being Division so I can filter the results by Division and Club.

My issue is that when I make a selection in the club field nothing happens, I can select from the Division field and the results will display the selected division but nothing happens when I select by club.

    <?php
include("config.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Club Search</title>
<style>
.style1 {
    text-align: center;
}
</style>
</head>
<body>
<fieldset style="width: 327px"><legend>Filter Results</legend>
<form id="filter" name="filter" method="post" action="search.php" style="width: 316px">
<label for="club">Club</label>
<select name="club">
<option value="">--</option>
<?php
    $sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY club ORDER BY club";
    $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
    while ($row = mysql_fetch_assoc($sql_result)) {
        echo "<option value='".$row["club"]."'".($row["club"]==$_REQUEST["club"] ? " selected" : "").">".$row["club"]."</option>";
    }
?>
</select>

    <label>Division</label>
<select name="division">
<option value="">--</option>
<?php
    $sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY division ORDER BY division";
    $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
    while ($row = mysql_fetch_assoc($sql_result)) {
    echo "<option value='".$row["division"]."'".($row["division"]==$_REQUEST["division"] ? " selected" : "").">".$row["division"]."</option>";
    }
?>
</select>
<input type="submit" name="button" id="button" value="Filter" />

  <a href="search.php"> 
  reset</a>
</form></fieldset>
<br /><br />
<fieldset style="width: 720px"><legend>Search Results</legend>
<br/>
<table width="700" border="1" cellspacing="0" cellpadding="0" align="center">
  <tr>
    <td bgcolor="#CCCCCC" class="style1"><strong>Division</strong></td>
    <td bgcolor="#CCCCCC" class="style1"><strong>Club</strong></td>
    <td bgcolor="#CCCCCC" class="style1"><strong>Last Name</strong></td>
    <td bgcolor="#CCCCCC" class="style1"><strong>First Name</strong></td>
    <td bgcolor="#CCCCCC" class="style1"><strong>ID</strong></td>
  </tr>
<?php
if ($_REQUEST["club"]='') {
    $search_string = " AND (club LIKE '%".mysql_real_escape_string($_REQUEST["club"])."%' OR (division LIKE '%".mysql_real_escape_string($_REQUEST["division"])."%')";  
}
if ($_REQUEST["division"]<>'') {
    $search_division = " AND division='".mysql_real_escape_string($_REQUEST["division"])."'";   
}

if ($_REQUEST["club"]='' and $_REQUEST["division"]='') {
    $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE club = '".mysql_real_escape_string($_REQUEST["club"])."' AND division = '".mysql_real_escape_string($_REQUEST["division"])."'".$search_string.$search_division;
}  else {
    $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id>0".$search_string.$search_division;
}

$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
    while ($row = mysql_fetch_assoc($sql_result)) {
?>
  <tr>
    <td class="style1"><?php echo $row['division']?></td>
    <td class="style1"><?php echo $row['club']?></td>
    <td class="style1"><?php echo $row['last_name']?></td>
    <td class="style1"><?php echo $row['first_name']?></td>
    <td class="style1"><?php echo $row['id']?></td>
  </tr>
<?php
    }
} else {
?>
<tr><td colspan="5">No results found.</td></tr>
<?php   
}
?>
</table>
<br/>
</fieldset>
</body>
</html>

UPDATE: I changed the following code from this:

if ($_REQUEST["club"]='') {
$search_string = " AND (club LIKE '%".mysql_real_escape_string($_REQUEST["club"])."%' OR (division LIKE '%".mysql_real_escape_string($_REQUEST["division"])."%')";  

}

to this:

if ($_REQUEST["club"]<>'') {
    $search_club = " AND (club LIKE '%".mysql_real_escape_string($_REQUEST["club"])."%' OR (club LIKE '%".mysql_real_escape_string($_REQUEST["club"])."%')";    
}

and now it will allow me to filter by club and division but I still can't do just by club alone!

4

2 回答 2

1

Change this:

if ($_REQUEST["club"]='') {
$search_string = " AND (club LIKE '%".mysql_real_escape_string($_REQUEST["club"])."%' OR (division LIKE '%".mysql_real_escape_string($_REQUEST["division"])."%')";

and this:

$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id>0".$search_string.$search_division;

To this:

if ($_REQUEST["club"]<>'') {
    $search_club = " AND club='".mysql_real_escape_string($_REQUEST["club"])."'";   
}

And this:

$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id>0".$search_club.$search_division;

And it will work!

于 2013-07-30T03:37:11.663 回答
0

You are using = operator in your if statements, you want to use == comparison operator like follows:

if ($_REQUEST["club"] == '') {
    $search_string = " AND (club LIKE '%".mysql_real_escape_string($_REQUEST["club"])."%' OR (division LIKE '%".mysql_real_escape_string($_REQUEST["division"])."%')";  
}
if ($_REQUEST["division"] <> '') {
    $search_division = " AND division='".mysql_real_escape_string($_REQUEST["division"])."'";   
}

if ($_REQUEST["club"] == '' and $_REQUEST["division"] == '') {
    $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE club = '".mysql_real_escape_string($_REQUEST["club"])."' AND division = '".mysql_real_escape_string($_REQUEST["division"])."'".$search_string.$search_division;
}  else {
    $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id>0".$search_string.$search_division;
}
于 2013-07-30T00:08:39.397 回答