我尝试在我的PHP项目中实现搜索按钮,但它不起作用并且始终显示错误消息。我无法连接我的数据库。
这是我的代码:
<?php
$db='test';
$conn=mysqli_connect('localhost','root','',$db);
if(isset($_POST["submit"])){
if ($_POST["place"]) {
$search_type = " AND place LIKE '%".$_POST["place"]."%'";
}
if ($_POST["bedrooms"]) {
$search_bedrooms = " AND bedroom='".$_POST["bedrooms"]."'";
}
if ($_POST["minrate"] and $_POST["maxrate"]) {
$sql = "SELECT * FROM upload WHERE rate >= '".$rate1."' AND rate <= ".$rate2."'".$search_type.$search_bedrooms;
} else if ($_POST["minrate"]) {
$sql = "SELECT * FROM upload WHERE rate >= '".$rate1."'".$search_type.$search_bedrooms;
} else if ($_POST["maxrate"]) {
$sql = "SELECT * FROM upload WHERE rate <= '".$rate2."'".$search_type.$search_bedrooms;
} else {
$sql = "SELECT * FROM upload WHERE id>0 " .$search_type.$search_bedrooms;
}
//$sql = rtrim($sql, " AND ");
$sql_result = mysqli_query ($conn,$sql) or die ('POST "Could not execute SQL query" '.$sql);
if ($count=mysqli_num_rows($sql_result)) {
while($row = mysqli_fetch_array($sql_result)) {
echo $row['place'] . "<br />";
echo $row['bedroom'] . "<br />";
echo $row['rate'] . "<br />";
?><td><img src="../uploads/post/ <?php echo $row['image']; ?>" style='width:100px;'><br><br>
<?php
}
}
}
?>
and this is my html code.
<form method="post" action="">
<tr>
<td>minimum rate:</td>
<td><select name="minrate" >
<option value="">select</option>
<option value="125">125</option>
<option value="357">357</option>
<option value="500">500</option></select>
</td>
<td>max rate:</td>
<td><select name="maxrate" required>
<option value="select">select</option>
<option value="125">125</option>
<option value="357">357</option>
<option value="500">500</option></select>
</td>
<td>bedrooms:</td>
<td><select name="bedrooms">
<option value="select">select</option>
<option value="3">3</option>
<option value="6">6</option>
<option value="2">2</option>
</select>
</td>
<td>place:</td>
<td><select name="place" />
<option value="any">any</option>
<option value="kannur">kannur</option>
<option value="calct">calicut</option>
<option value="tly">telicherry</option>
</select>
</td>
<td> </td>
<td><input type="submit" name="submit" value="Search" /></td>
</tr>
</form>
</table>
我检查了我的代码超过3次。我看不到任何连接问题。