简体   繁体   中英

Search using PHP PDO

I'm new to PHP, and I'm trying to create CRUD with a search using PDO. But I got a problem with my code. When I filtered the data the fetch result didn't return in the HTML table output. Instead, the result data returning on a separate table. I hope someone can help me to solve this. Thank you in advance.

Here is my search.php code

<?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>

Here is my index.php code

<!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>

If a browser sees that the html for a <table> element is broken, it can lead to unexpected results. One of those results can be that some of the elements get "hoisted" out of the original table element into a separate one. I believe that is what is happening here.

You should look carefully at your html output and determine if there are any missing or extra tags. Likely, you have an tag somewhere that is an invalid child of a <table> or <tr> .

Also, you can't use the developer tools for this, as that will show the "corrected" html, not the original html.

<form action="search.php" method="POST">
    <input type="text" name="keyword"/>
    <button name="search"> Search </button>
</form>

I didn't go through all the codes but i saw this: you are missing form action which should be search.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM