所以我试图制作一个会填满我的桌子的搜索栏,但我的代码似乎无法正常工作
<?php $connection = mysqli_connect("localhost","root","","e-learning") or
die ("Error");
$query = "SELECT * FROM admin_info";
$queryResult = mysqli_query ($connection,$query);
if(isset($_GET ['submit'])&& !empty($_GET['submit'])){
$safe_value = $GET['search'];
$query = "SELECT * FROM admin_info
WHERE`first_name`like'"."%".$safe_value."%"."'";
$queryResult = mysqli_query ($connection,$query);
}
?>
这是我的HTML代码:
<form method="GET">
<div class="input-group">
<input name="search" id="search" type="text" class="form-control" placeholder="Search for...">
<span class="input-group-btn">
<button type="submit" name="submit" id="submit" class="btn btn-secondary" type="button">Go!</button>
</span>
</form>
这是我的表
<?php
while ($row = mysqli_fetch_array($queryResult)) {?>
<tr>
<td style="overflow:hidden;white-space:nowrap;"><?php echo $row['id'];?></td>
<td style="overflow:hidden;white-space:nowrap;"><?php echo $row['first_name']." ".$row['middle_name']." ".$row['last_name'] ?></td>
<td style="overflow:hidden;white-space:nowrap;"><?php echo $row['user_level'];?></td>
<td style="overflow:hidden;white-space:nowrap;"><?php echo $row['status'];?></td>
</tr>
<?php }
?>
ps:我从第一个查询得到输出但是每当我点击go搜索按钮我都无法得到第二个
答案 0 :(得分:0)
有几件事情......
<button type="submit" name="submit" id="submit" class="btn btn-secondary" type="button">Go!</button>
,因此请删除type =“button”。<form method="GET">
转换为<form action = "<?php $_PHP_SELF ?>" method = "GET">
,因为PHP脚本与html代码位于同一页面上。if(isset($_GET ['submit'])&& !empty($_GET['submit'])){
替换if(isset($_GET ['search'])&& !empty($_GET['search'])){
$safe_value = $GET['search'];
,因为您应该$_GET['search']
但是,如果您仔细查看代码,所有这些都可能被您发现。
以下是完整的功能代码:
<?php
$connection = mysqli_connect("localhost","root","","e-learning") or
die ("Error");
$query = "SELECT * FROM admin_info";
$queryResult = mysqli_query ($connection,$query);
if(isset($_GET ['search'])&& !empty($_GET['search'])){
$safe_value = $_GET['search'];
$query = "SELECT * FROM admin_info
WHERE`first_name`like'"."%".$safe_value."%"."'";
$queryResult = mysqli_query ($connection,$query);
}
?>
<html>
<body>
<form action = "<?php $_PHP_SELF ?>" method = "GET">
<div class="input-group">
<input name="search" id="search" type="text" class="form-control" placeholder="Search for...">
<span class="input-group-btn">
<button type="submit" class="btn btn-secondary">Go!</button>
</span>
</form><br/><br/>
<?php
while ($row = mysqli_fetch_array($queryResult)) { ?>
<tr>
<td style="overflow:hidden;white-space:nowrap;"><?php echo $row['id'];?></td>
<td style="overflow:hidden;white-space:nowrap;"><?php echo $row['first_name']." ".$row['middle_name']." ".$row['last_name'] ?></td>
<td style="overflow:hidden;white-space:nowrap;"><?php echo $row['user_level'];?></td>
<td style="overflow:hidden;white-space:nowrap;"><?php echo $row['status'];?></td>
</tr><br/>
<?php } ?>
</body>
</html>