使用PHP PDO进行搜索

使用PHP PDO进行搜索,php,mysql,Php,Mysql,我是PHP新手,我正在尝试使用PDO通过搜索创建CRUD。但我的代码有问题。当我过滤数据时,提取结果没有在HTML表输出中返回。相反,结果数据返回到一个单独的表中。我希望有人能帮我解决这个问题。先谢谢你 这是我的search.php代码 这是我的index.php代码 我没有浏览所有的代码,但我看到了:您缺少表单操作,应该是search.php如果浏览器看到某个元素的html被破坏,可能会导致意外的结果。其中一个结果可能是一些元素被从原始表元素提升到单独的表元素中。我相信这就是这里正在发生的事情

我是PHP新手,我正在尝试使用PDO通过搜索创建CRUD。但我的代码有问题。当我过滤数据时,提取结果没有在HTML表输出中返回。相反,结果数据返回到一个单独的表中。我希望有人能帮我解决这个问题。先谢谢你

这是我的search.php代码

这是我的index.php代码


我没有浏览所有的代码,但我看到了:您缺少表单操作,应该是search.php

如果浏览器看到某个元素的html被破坏,可能会导致意外的结果。其中一个结果可能是一些元素被从原始表元素提升到单独的表元素中。我相信这就是这里正在发生的事情

您应该仔细查看html输出,并确定是否有任何缺少或额外的标记。很可能,您在某个地方有一个标记,它是or的无效子级


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

您是说,当打印搜索结果html时,顶部有一个表,其中有标题行,然后在它下面的另一个表中显示数据行?@JakeParis是的,你是对的。你能复制搜索结果的html并粘贴到这里吗?我很确定你的问题是表格的html放错了地方。我看不出search.php代码中有任何问题,但这可能就是问题所在。您的数据库值可能包含任何html吗?@JakeParis,在这里。这只是我的练习样本:名字姓氏年龄电子邮件samplie samplie 23samplie@gmail.comNo... 如果您查看OP的html底部,search.php是一个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>
<!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>
<form action="search.php" method="POST">
    <input type="text" name="keyword"/>
    <button name="search"> Search </button>
</form>