我正在尝试制作一个带有复选框的搜索过滤器。当我显示表中的所有数据时,它显示得很好,但当我尝试使用复选框时,什么都不会发生。这是我的密码。
<?php
include "connect.php";
$sql="SELECT * FROM websiteusers ";
if (isset($_POST['submit'])) {
$searchgender=$_POST['gender'];
$searchethnicity=$_POST['ethnicity'];
$sql .= "WHERE gender = '{$searchgender}' ";
$sql .= " AND ethnicity = '{$searchethnicity}'";
}
$query=mysql_query($sql) or die(mysql_error());
?>
<form name="form" action="search.php" method="post">
<table>
<tr>
<td>
Gender:
</td>
<td>
<input type="checkbox" name="gender" value="male">Male<br>
<input type="checkbox" name="gender" value="female">Female
</td>
</tr>
<tr>
<td>
Ethnicity:
</td>
<td>
<input type="checkbox" name="ethnicity" value="black">Black<br>
<input type="checkbox" name="ethnicity" value="hispanic">Hispanic
</td>
</tr>
</table>
<p><input type="submit" value="search profiles" name="search"></p>
</form>
将if (isset($_POST['search'])) {
(请参阅以下注释)放在上面
CCD_ 2。
您也没有一个名为submit
的表单元素,但您有一个名称为search
的提交按钮
<input type="submit" value="search profiles" name="search">
因此,将if (isset($_POST['submit']))
更改为if (isset($_POST['search']))
,这很可能是它不适用于您的原因。
代码的执行基于该条件语句。
启用错误报告会发出Undefined index submit...
信号
error_reporting(E_ALL);
ini_set('display_errors', 1);
您还需要使用while
循环遍历结果。
类似的东西
while($results = mysql_fetch_array($query)){
echo $results['gender'] . "<br>" $results['ethnicity'] . "<br>";
}
编辑
这是一个mysqli_*
方法,而不是我测试和工作的mysql_*
方法。还包括一个三元运算符和用于搜索变量的mysqli_real_escape_string()
。
符号:如果未选中其中一个复选框,三元运算符将避免收到Undefined index...
警告。
将search.php
更改为action=""
,因为所有内容都在同一页中。
旁注:您可能想在此行中使用OR
而不是AND
,但已使用AND
-OR
已在下面注释:
$sql .= " AND ethnicity = '{$searchethnicity}'";
代码:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$DB_HOST = "localhost"; // replace but do not use mysql_ method.
$DB_NAME = "xxx"; // replace
$DB_USER = "xxx"; // replace
$DB_PASS = "xxx"; // replace
$conn = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($conn->connect_errno > 0) {
die('Connection failed [' . $conn->connect_error . ']');
}
$sql="SELECT * FROM websiteusers ";
if (isset($_POST['submit'])) {
$searchgender = isset($_POST['gender']) ? mysqli_real_escape_string($conn,$_POST['gender']) : '';
$searchethnicity = isset($_POST['ethnicity']) ? mysqli_real_escape_string($conn,$_POST['ethnicity']) : '';
$sql .= "WHERE gender = '{$searchgender}' ";
// $sql .= " OR ethnicity = '{$searchethnicity}'"; // either use this
$sql .= " AND ethnicity = '{$searchethnicity}'"; // or this, not both
}
$query=mysqli_query($conn,$sql) or die(mysqli_error($conn));
?>
<form name="form" action="" method="post">
<table>
<tr>
<td>
Gender:
</td>
<td>
<input type="checkbox" name="gender" value="male">Male<br>
<input type="checkbox" name="gender" value="female">Female
</td>
</tr>
<tr>
<td>
Ethnicity:
</td>
<td>
<input type="checkbox" name="ethnicity" value="black">Black<br>
<input type="checkbox" name="ethnicity" value="hispanic">Hispanic
</td>
</tr>
</table>
<p><input type="submit" value="search profiles" name="submit"></p>
</form>
<table width="70%" cellspacing="5" cellpadding="5">
<tr>
<td><strong>name</strong></td>
<td><strong>gender</strong></td>
<td><strong>ethnicity</strong></td>
</tr>
<?php while ($row=mysqli_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['gender']; ?></td>
<td><?php echo $row['ethnicity']; ?></td>
</tr>
<?php } ?>
</table>
脚注:
为了使
AND
正常工作,需要选中每个复选框中的一个。如果所有四个复选框都被勾选,那么它将不适用于
$sql="SELECT * FROM websiteusers ";
0。您需要使用OR
才能正常工作或重新构建查询数据库的方式。
您甚至可能想尝试使用OR
或AND
的LIKE
子句,这取决于您希望查询的条件。
$sql .= "WHERE gender LIKE '{$searchgender}' ";
$sql .= " OR ethnicity = '{$searchethnicity}'";
组合太多,无法包含。你需要尝试一下,然后把它们混合在一起。
<?php
include "connect.php";
$sql="SELECT * FROM websiteusers WHERE 0 OR ";
if (isset($_POST['submit'])) {
if (isset($_POST['gender'])){
$searchgender_raw= $_POST['gender'];
foreach ($sg in $searchgender_raw)
$searchgender.= " '" . $sg . "',";
}
$searchgender = rtrim($searchgender, ",");
if (isset($_POST['ethnicity'])){
$searchethnicity_raw= $_POST['ethnicity'];
foreach ($se in $searchethnicity_raw)
$searchethnicity.= " '" . $se . "',";
}
$searchethnicity = rtrim($searchethnicity, ",");
$sql .= " WHERE gender IN ({$searchgender}) ";
$sql .= " AND ethnicity IN ({$searchethnicity})";
}
$query=mysql_query($sql) or die(mysql_error());
?>
现在让我知道,如果你不理解代码
编辑:新代码:你好,这是我的新代码,男女过滤器有效,但种族给了我"未定义的索引"。您还会注意到我有两个查询行。这是因为底部的代码显然无法到达代码第一位中的那个。好的,这是我的新代码。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$DB_HOST = "localhost";
$DB_NAME = "mysite";
$DB_USER = "root";
$DB_PASS = "***"; // password edited out
$conn = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($conn->connect_errno > 0) {
die('Connection failed [' . $conn->connect_error . ']');
}
$sql="SELECT * FROM websiteusers ";
if (isset($_POST['submit'])) {
$searchgender = isset($_POST['gender']) ? mysqli_real_escape_string($conn,$_POST['gender']) : '';
$searchethnicity = isset($_POST['ethnicity']) ? mysqli_real_escape_string($conn,$_POST['gender']) : '';
$sql .= "WHERE gender = '{$searchgender}'";
$sql .= " OR ethnicity = '{$searchethnicity}' ";
}
$query=mysqli_query($conn,$sql) or die(mysqli_error($conn));
?>
<?php
$query=mysqli_query($conn,$sql) or die(mysqli_error($conn));
?>
<form name="form" action="" method="post">
<table>
<tr>
<td>
Gender:
</td>
<td>
<input type="checkbox" name="gender" value="male">Male<br>
<input type="checkbox" name="gender" value="female">Female
</td>
</tr>
<tr>
<td>
Ethnicity:
</td>
<td>
<input type="checkbox" name="ethnicity" value="black">Black<br>
<input type="checkbox" name="ethnicity" value="hispanic">Hispanic
</td>
</tr>
</table>
<p><input type="submit" value="search profiles" name="submit"></p>
</form>
<table width="70%" cellspacing="5" cellpadding="5">
<tr>
<td><strong>name</strong></td>
<td><strong>gender</strong></td>
<td><strong>ethnicity</strong></td>
</tr>
<?php while ($row=mysqli_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['gender']; ?></td>
<td><?php echo $row['ethnicity']; ?></td>
</tr>
<?php } ?>
</table>