在“搜索”后显示结果

我遇到了问题,我想创建一个搜索框来从数据库中搜索记录。但是“ index.php”显示了所有加载的记录。我希望它只显示我搜索的记录。

'index.php'     

if(isset($_POST['search']))
{
    $valueToSearch = $_POST['valueToSearch'];
    // search in all table columns
    // using concat mysql function
    $query = "SELECT * FROM `test` WHERE concat(`id`,`naam`,`website`) LIKE '%".$valueToSearch."%'";
    $search_result = filterTable($query);

}
 else {
    $query = "SELECT * FROM `test`";
    $search_result = filterTable($query);
}

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

?>

<!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="index.php" method="post">
            <input type="text" name="valueToSearch" placeholder="Value To Search"><br><br>
            <input type="submit" name="search" value="Filter"><br><br>

            <table>
                <tr>
                    <th>Id</th>
                    <th>Servers</th>
                    <th>Website</th>
                </tr>

      <!-- populate table from mysql database -->
                <?php while($row = mysqli_fetch_array($search_result)):?>
                <tr>
                    <td><?php echo $row['id'];?></td>
                    <td><?php echo $row['naam'];?></td>
                    <td><?php echo $row['website'];?></td>
                </tr>
                <?php endwhile;?>
            </table>
        </form>

    </body>
</html>

我想当我什么都不输入时什么也不会出现,而是出现所有记录。

有人可以帮助我吗?

haixiakeji 回答:在“搜索”后显示结果

if(isset($_POST['search']))
{
    $valueToSearch = $_POST['valueToSearch'];
    $query = "SELECT * FROM `test`";
    if($_POST['valueToSearch'] != "")
    { 
        $query .= " WHERE CONCAT(`id`,`naam`,`website`) LIKE '%".$valueToSearch."%'";
    }
    $search_result = filterTable($query);
}
本文链接:https://www.f2er.com/3168225.html

大家都在问