我正在尝试过滤我的MySQL数据库的搜索结果,我想通过从两个下拉框中选择值来过滤结果,第一个框是Club,第二个框是Division,所以我可以过滤结果分部和俱乐部。
我的问题是,当我在俱乐部区域进行选择时没有任何反应,我可以从分区区域中进行选择,结果将显示所选分区,但是当我选择俱乐部时没有任何反应。
<?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>
更新: 我从这里更改了以下代码:
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["club"]<>'') {
$search_club = " AND (club LIKE '%".mysql_real_escape_string($_REQUEST["club"])."%' OR (club LIKE '%".mysql_real_escape_string($_REQUEST["club"])."%')";
}
现在它将允许我按俱乐部和分区过滤但我仍然不能仅靠俱乐部来做!
答案 0 :(得分:1)
改变这个:
if ($_REQUEST["club"]='') {
$search_string = " AND (club LIKE '%".mysql_real_escape_string($_REQUEST["club"])."%' OR (division LIKE '%".mysql_real_escape_string($_REQUEST["division"])."%')";
和此:
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id>0".$search_string.$search_division;
对此:
if ($_REQUEST["club"]<>'') {
$search_club = " AND club='".mysql_real_escape_string($_REQUEST["club"])."'";
}
而且:
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id>0".$search_club.$search_division;
它会起作用!
答案 1 :(得分:0)
您在=
语句中使用if
运算符,您希望使用==
比较运算符,如下所示:
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;
}