【问题标题】:Php/SQL Table Search with Space带空格的 PHP/SQL 表搜索
【发布时间】:2022-01-12 23:46:53
【问题描述】:

我设置了一个 php 文件来从数据库中提取数据,并将其列在表上。然后我制作了一个简单的搜索功能来搜索该数据,但是我不知道如何在搜索时实现空格。例如,如果我搜索测试 2(从一列测试,从另一列测试 2)它不会显示任何内容。但是,如果我要搜索“test2”或“2test”,它会显示所有结果。我将如何在我的代码中实现该空间?

这是我的代码:


if(isset($_POST['search']))
{
    $valueToSearch = $_POST['valueToSearch'];
    // search in all table columns
    // using concat mysql function
    $query = "SELECT * FROM `master` WHERE CONCAT_WS(`id`, `office`, `firstName`, `lastName`, `type`, `status`, `deadline`, `contactPref`, `email`, `phoneNumber`, `taxPro`) LIKE '%".$valueToSearch."%'";
    $search_result = filterTable($query);
    
}
 else {
    $query = "SELECT * FROM `master`";
    $search_result = filterTable($query);
}

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

?>

<!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="Untitled-1.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>Office</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Type</th>
                    <th>Status</th>
                    <th>Deadline</th>
                    <th>Contact Preference</th>
                    <th>Email</th>
                    <th>Phone Number</th>
                    <th>Tax Pro</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['office'];?></td>
                    <td><?php echo $row['firstName'];?></td>
                    <td><?php echo $row['lastName'];?></td>
                    <td><?php echo $row['type'];?></td>
                    <td><?php echo $row['status'];?></td>
                    <td><?php echo $row['deadline'];?></td>
                    <td><?php echo $row['contactPref'];?></td>
                    <td><?php echo $row['email'];?></td>
                    <td><?php echo $row['phoneNumber'];?></td>
                    <td><?php echo $row['taxPro'];?></td>
                </tr>
                <?php endwhile;?>
            </table>
        </form>
        
    </body>
</html>```

【问题讨论】:

    标签: php sql search


    【解决方案1】:

    这样做的一种方法是让你的字符串溢出到 php.ini 中的单词中。您可以为此使用爆炸功能 (https://www.php.net/manual/en/function.explode.php)

    然后像这样编写 SQL where 子句(我假设您使用的是 mysql): WHERE col LIKE %word% OR col LIKE %word2%

    这样效率会有点低,也许你要找的工具是elasticsearch? https://www.elastic.co/what-is/elasticsearch

    另外,你不应该使用 .对不可信输入的操作员。因为它创建了 MySQL 注入漏洞。请参阅此问题以供参考:How can I prevent SQL injection in PHP?

    【讨论】:

      【解决方案2】:

      最简单的解决方案是在您的 mysql 字段中使用 FULLTEXT index。但我不建议在生产中使用它。相反,对于实际项目,必须考虑外部搜索引擎(例如 Elasticsearch 等)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-08
        相关资源
        最近更新 更多