我正在尝试使用复选框进行搜索筛选,但是mysql OR和AND条件非常混乱,我如何设置它以显示用户检查的所有结果?我的代码。复选框mysql条件混淆:(
<?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['ethnicity']) : '';
$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>
//more code (it says too much code lol)
能否请您详细说明你做了什么? – babyleans 2014-09-22 11:15:06