我是PHP的新手,我正在尝试使用PDO创建带有搜索的CRUD。 但我的代码有问题。 当我过滤数据时,提取结果没有在HTML表输出中返回。 而是在单独的表上返回的结果数据。 我希望有人能帮我解决这个问题。 提前谢谢你。
下面是我的search.php代码
<?php
require_once 'config.php';
if (isset($_POST['search'])) {
?>
<table>
<thead>
<tr>
<th> First name</th>
<th> Last name</th>
<th> Age</th>
<th> Email</th>
</tr>
</thead>
<tbody>
<?php
$keyword=$_POST['keyword'];
$query=$dbConn->prepare("SELECT * FROM user WHERE first_name LIKE '$keyword' or last_name LIKE '$keyword' or age LIKE '$keyword' or email LIKE '$keyword' ");
$query->execute();
while($row=$query->fetch()) { ?>
<tr>
<td> <?php echo $row['first_name']; ?> </td>
<td> <?php echo $row['last_name']; ?> </td>
<td> <?php echo $row['age']; ?> </td>
<td> <?php echo $row['email']; ?> </td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
} else ?>
<table>
<thead>
<tr>
<th> First name</th>
<th> Last name</th>
<th> Age</th>
<th> Email</th>
</tr>
</thead>
<tbody>
<?php
$query=$dbConn->prepare("SELECT * FROM user");
$query->execute();
while($row=$query->fetch()) {
?>
<tr>
<td> <?php echo $row['first_name']; ?> </td>
<td> <?php echo $row['last_name']; ?> </td>
<td> <?php echo $row['age']; ?> </td>
<td> <?php echo $row['email']; ?> </td>
</tr>
<?php } ?>
</tbody>
null
下面是我的index.php代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Index Php</title>
</head>
<body>
<form action="insert.php" method="POST">
<div class="form-group">
<label> Firstname </label>
<input type="text" name="first_name" class="form-control" required />
</div>
<div class="form-group">
<label> Lastname </label>
<input type="text" name="last_name" class="form-control" required />
</div>
<div class="form-group">
<label> Age </label>
<input type="text" name="age" class="form-control" required />
</div>
<div class="form-group">
<label> Email</label>
<input type="text" name="email" class="form-control" required />
</div>
<div class="form-group">
<input type="submit" name="submit" value="Submit" class="form-control">
</div>
</form>
<br />
<form action="" method="POST">
<input type="text" name="keyword"/>
<button name="search"> Search </button>
</form>
<br /> <br />
<?php include('search.php'); ?>
</body>
</html>
在使用用户输入运行数据库查询时,应使用prepare语句。
$query = dbConn()->prepare("SELECT * FROM user WHERE firstname LIKE '?'");
if($query->execute([$keyword]))
{
while($row=$query->fetch()){
print($row);
}
如果浏览器看到 您应该仔细查看您的html输出,并确定是否有任何缺失或额外的标记。 您可能在某个地方有一个标记,它是 此外,您不能使用开发人员工具来实现这一点,因为这将显示“更正”的html,而不是原始的html。
我没有看完所有的代码,但是我看到了:您缺少了应该是search.php的表单操作元素的html中断,则可能导致意外结果。 其中一个结果可能是,一些元素从原始表元素中“提升”到一个单独的表元素中。 我相信这就是这里正在发生的事情。
或
的无效子项。
<form action="search.php" method="POST">
<input type="text" name="keyword"/>
<button name="search"> Search </button>
</form>
相关问题