首页 > 解决方案 > 使用 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>

标签: phpmysql

解决方案


如果浏览器发现某个元素的 html<table>已损坏,则可能会导致意外结果。这些结果之一可能是某些元素从原始表格元素中“提升”到一个单独的元素中。我相信这就是这里正在发生的事情。

您应该仔细查看您的 html 输出并确定是否有任何缺失或多余的标签。很可能,您在某处有一个标签,它是 a<table>或的无效子代<tr>

此外,您不能为此使用开发人员工具,因为这将显示“更正”的 html,而不是原始 html。


推荐阅读