我正在开发一个项目,我需要从html下拉列表中选择选项,然后显示从数据库中选择的数据。就像过滤并显示来自数据库的过滤数据一样。 还是行不通 我得到错误:
它只是显示来自db的所有数据,而不是通过过滤查询
<form action="search.php" method="POST">
<div class="md-form">
<select class="mdb-select" name="state_search" required>
<option disabled selected>Choose your State</option>
<option name="state" value="Abia State">Abia State</option>
<option name="state" value="Adamawa State">Adamawa State</option>
<option name="state" value="Anambra State">Anambra State</option>
</select>
</div>
<div class="col-lg-4">
<div class="md-form">
<select class="mdb-select" name="school_search">
<option value="" disabled selected>Choose your School</option>
<option value="Michael Opkara University">Mouau</option>
<option value="University of Benin">UniBen</option>
<option value="University of Porthacourt">UniPort</option>
</select>
</div>
<button type="submit" class="btn purple-gradient btn-lg" name="filter">Search <i class="fa fa-paper-plane-o ml-1"></i></button>
</div>
</form>
PHP
<?php
$school_search = $connect->real_escape_string($_POST["school_search"]);
$state_search = $connect->real_escape_string($_POST["state_search"]);
$query = mysqli_query($connect, "SELECT * FROM roomate WHERE (`state` LIKE '%".$state_search."%') OR (`school` LIKE '%".$school_search."%')") or die(mysql_error());
if($query){
while($row = mysqli_fetch_array($query)){
?>
<div class="container" style="padding-top:70px;">
<!--Grid row-->
<div class="row">
<?php
foreach ($query as $user) {?>
<div class="col-lg-4 col-md-12 mb-r">
<!--Featured image-->
<div class="overlay hm-white-slight z-depth-1-half mb-2">
<img src="<?php echo $user['room_avatar']?>" class="img-fluid" alt="avatar image">
<a><div class="mask"></div></a>
</div>
<!--Excerpt-->
<a href="" class="pink-text">
<h6>
<i class="fa fa-map"></i><strong><?php echo $user['city'];?>, <?php echo $user['state'];?></strong><br>
<small><?php echo $user['school'];?></small>
</h6>
</a>
<h4><?php echo $user['type'];?></h4>
<p>
by <a><strong><?php echo $user['room_name'];?></strong></a>
<?php echo $user['created'];?>
</p>
<label class="badge badge-danger"> non-verified </label>
<a href="contact.php">
<label class="badge badge-primary">Report Scam</label>
</a>
<p><?php echo $user['discription'];?></p>
<a class="btn btn-pink btn-rounded">Contact <?php echo $user['room_name'];?></a>
</div>
<?php
} ?>
</div>
</div>
<?php
}
}
else{
echo(mysql_error());
}
?>
需要帮助才能完成这项工作
答案 0 :(得分:0)
select
的名称需要与$_POST
变量键匹配。
因此,在您的情况下,<select>
需要名称state_search
。
或将您的$_POST
搜索更改为$_POST['state']
以匹配您的选择名称。
您还应该在查询中使用$state_search
变量而不是$query
。
您可能会遇到更多问题,只需注意细节。
答案 1 :(得分:0)
你在echo函数中添加了很多HTML。
<?php
$school_search = $connect->real_escape_string($_POST["school_search"]);
$state_search = $connect->real_escape_string($_POST["state_search"]);
$query = mysqli_query($connect, "SELECT * FROM roomate WHERE (`room_name` LIKE '%".$query."%') OR (`discription` LIKE '%".$query."%') OR (`discription` LIKE '%".$query."%') OR (`type` LIKE '%".$query."%')") or die(mysql_error());;
$result = mysqli_query($connect, $query);
if($result){
while($row = mysqli_fetch_array($result)){
?>
<div class="container" style="padding-top:70px;">
<!--Grid row-->
<div class="row">
<?php
foreach ($query as $user) {?>
<div class="col-lg-4 col-md-12 mb-r">
<!--Featured image-->
<div class="overlay hm-white-slight z-depth-1-half mb-2">
<img src="<?php echo $user['room_avatar']?>" class="img-fluid" alt="avatar image">
<a><div class="mask"></div></a>
</div>
<!--Excerpt-->
<a href="" class="pink-text">
<h6>
<i class="fa fa-map"></i><strong><?php echo $user['city'];?>, <?php echo $user['state'];?></strong><br>
<small><?php echo $user['school'];?></small>
</h6>
</a>
<h4><?php echo $user['type'];?></h4>
<p>
by <a><strong><?php echo $user['room_name'];?></strong></a>
<?php echo $user['created'];?>
</p>
<label class="badge badge-danger"> non-verified </label>
<a href="contact.php">
<label class="badge badge-primary">Report Scam</label>
</a>
<p><?php echo $user['discription'];?></p>
<a class="btn btn-pink btn-rounded">Contact <?php echo $user['room_name'];?></a>
</div>
<?php
} ?>
</div>
</div>
<?php
}
}
else{
echo(mysql_error());
}