使用 php html 中的表单过滤数据库 table

filter database table with form in php html

我正在尝试在 php/html 中制作一个简单的 search/filter 页面,但我遇到了这两个错误,我有几个小时试图修复它,但我就是做不到(我'我刚开始学习,我正在学习教程,但我无法让它工作)

Notice: Undefined variable: search_result in /storage/ssd1/909/16765909/public_html/db/db_search.php on line 34

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /storage/ssd1/909/16765909/public_html/db/db_search.php on line 34

到目前为止,这是我的代码

<!DOCTYPE html>
<html>
    <head>
        <title>PHP HTML TABLE DATA SEARCH</title>
        <style>
            table,tr,th,td
            {
                border: 1px solid black;
            }
        </style>
    </head>
    <body>
        
        <form action="db_search.php" method="post">
            <input type="text" name="valueToSearch" placeholder="Value To Search"><br><br>
            <input type="submit" name="search" value="Filtrar"><br><br>
            
            <table border="2" >
        <tr>
            <td>ID</td>
            <td>Cédula</td>
            <td>Nombre</td>
            <td>Apellido</td>
            <td>Género</td>
            <td>Año de Nacimiento</td>
            <td>Correo</td>
            <td>Desde</td>
    <td>Edit</td>
    <td>Delete</td>
    </tr>

      <!-- populate table from mysql database -->
                <?php 
                while($row=mysqli_fetch_array($search_result)):?>
                <tr>
            <td><?php echo $row['user_id'] ?></td>
            <td><?php echo $row['user_ssn'] ?></td>
            <td><?php echo $row['user_name'] ?></td>
            <td><?php echo $row['user_lastname'] ?></td>
            <td><?php echo $row['user_gender'] ?></td>
            <td><?php echo $row['user_birthdate'] ?></td>
            <td><?php echo $row['user_mail'] ?></td>
            <td><?php echo $row['user_datestamp'] ?></td>
    <td><a href="/db/db_mod.php?id=<?php echo $row['user_id']; ?>">Edit</a></td>
    <td><a href="/db/db_del.php?id=<?php echo $row['user_id']; ?>">Delete</a></td>
        </tr>
                <?php endwhile;?>
            </table>
        </form>
        
    </body>
</html>

<?php

if(isset($_POST['search']))
{
    $valueToSearch = $_POST['valueToSearch'];
    // search in all table columns
    // using concat mysql function
    $query = "SELECT * FROM `user` WHERE CONCAT(`id`, `user_ssn`, `user_name`, `user_lastname`, `user_gender`, `user_birthdate`, `user_mail`, `user_datestamp`) LIKE '%".$valueToSearch."%'";
    $search_result = filterTable($query);
    
}
 else {
    $query = "SELECT * FROM `user`";
    $search_result = filterTable($query);
}

// function to connect and execute the query
function filterTable($query)
{
    $connect = mysqli_connect("localhost", "root", "", "practice");
    $filter_Result = mysqli_query($connect, $query);
    return $filter_Result;
}

?>
    </body>
</html>

首先你需要在渲染之前获取数据。

<!DOCTYPE html>
<html>
    <head>
        <title>PHP HTML TABLE DATA SEARCH</title>
        <style>
            table,tr,th,td
            {
                border: 1px solid black;
            }
        </style>
    </head>
    <body>
        <?php

        if(isset($_POST['search']))
        {
            $valueToSearch = $_POST['valueToSearch'];
            // search in all table columns
            // using concat mysql function
            $query = "SELECT * FROM `user` WHERE CONCAT(`id`, `user_ssn`, `user_name`, `user_lastname`, `user_gender`, `user_birthdate`, `user_mail`, `user_datestamp`) LIKE '%".$valueToSearch."%'";
            $search_result = filterTable($query);
            
        }
        else {
            $query = "SELECT * FROM `user`";
            $search_result = filterTable($query);
        }

        // function to connect and execute the query
        function filterTable($query)
        {
            $db = new mysqli("localhost", "root", "", "practice");
            if ($db->connect_error) {
                die("Connection failed: " . $db->connect_error);
            } 
            $filter_Result = $db->query($query) or die($db->error);
            return $filter_Result;
        }

        ?>

        <form action="db_search.php" method="post">
            <input type="text" name="valueToSearch" placeholder="Value To Search"><br><br>
            <input type="submit" name="search" value="Filtrar"><br><br>
            
            <table border="2" >
                <tr>
                    <td>ID</td>
                    <td>Cédula</td>
                    <td>Nombre</td>
                    <td>Apellido</td>
                    <td>Género</td>
                    <td>Año de Nacimiento</td>
                    <td>Correo</td>
                    <td>Desde</td>
                    <td>Edit</td>
                    <td>Delete</td>
                </tr>

            <!-- populate table from mysql database -->
                <?php 
                while($row=$search_result->fetch_assoc()):?>
                <tr>
                    <td><?php echo $row['user_id'] ?></td>
                    <td><?php echo $row['user_ssn'] ?></td>
                    <td><?php echo $row['user_name'] ?></td>
                    <td><?php echo $row['user_lastname'] ?></td>
                    <td><?php echo $row['user_gender'] ?></td>
                    <td><?php echo $row['user_birthdate'] ?></td>
                    <td><?php echo $row['user_mail'] ?></td>
                    <td><?php echo $row['user_datestamp'] ?></td>
                    <td><a href="/db/db_mod.php?id=<?php echo $row['user_id']; ?>">Edit</a></td>
                    <td><a href="/db/db_del.php?id=<?php echo $row['user_id']; ?>">Delete</a></td>
                </tr>
                <?php endwhile;?>
            </table>
        </form>
        
    </body>
</html>