php - 使用 PHP PDO 搜索
问题描述
我是 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>
这是我的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>
解决方案
如果浏览器发现某个元素的 html<table>
已损坏,则可能会导致意外结果。这些结果之一可能是某些元素从原始表格元素中“提升”到一个单独的元素中。我相信这就是这里正在发生的事情。
您应该仔细查看您的 html 输出并确定是否有任何缺失或多余的标签。很可能,您在某处有一个标签,它是 a<table>
或的无效子代<tr>
。
此外,您不能为此使用开发人员工具,因为这将显示“更正”的 html,而不是原始 html。
推荐阅读
- google-sheets - 有没有办法将链接粘贴到用于从中导入数据的外部单元格中?
- python - python 正则表达式在数字和给定字符之间添加空格
- java - Gradle 名称绑定任务类型
- python - 光线投射来计算空间的体积
- python - 我们如何使用 Spacy minibatch 和 GoldParse 使用 BILUO 标记方案来训练 NER 模型?
- printing - 从 Fortran 输出中的打印信息跟踪源代码中的行
- r - 根据 R 中的模式删除观察值
- spring - 从头开始阅读 Kafka 主题
- angular - NgRx 取消订阅 store in effects
- java - 上传的图片不显示在网页上