如何使用PHP和Mysql在数据库表中放置搜索框 [英] How to put search box in the database table using PHP and Mysql

查看:128
本文介绍了如何使用PHP和Mysql在数据库表中放置搜索框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在数据库中创建了客户信息".我还将其放在我的customer.php页面上并进行查看.帮我在页面内制作一些搜索框".搜索框将仅搜索和查看数据库表中最接近的关键字.

I've created the "Customers' Information" in the database. I also put and view it on my customers.php page. Help me to make some "Search box" inside the page. The search box will only search and view what keyword nearer in my database table.

示例:当我搜索"Jenny"时,搜索框的结果将是我的数据库表中的所有"Jenny".当我的搜索框找不到珍妮"时,搜索框会显示未找到结果"

Example: When I search "Jenny" the result of search box will be all "Jenny" in my database table. When my search box don't find "Jenny," the search box will say "no results found"

这是我在customers.php中的代码

Here is my code in customers.php

<?php
    $conn = mysqli_connect('localhost','root','','newcartdb');
        if(mysqli_connect_errno()){
            echo 'Failed to Connect: '.mysqli_connect_error();
        }

        if(isset($_POST['delete'])){
            $DeleteQuery = "DELETE FROM customers WHERE id='$_POST[hidden]'";
            mysqli_query($conn,$DeleteQuery);
        }

        $query = "SELECT * FROM customers ORDER BY id ASC";
        $results = mysqli_query($conn,$query);

            echo '<table class="table table-bordered">
                          <thead>
                            <tr>
                              <th width="40px">ID</th>
                              <th>Email</th>
                              <th>Firstname</th>
                              <th>Lastname</th>
                              <th>Gender</th>
                              <th>Titlename</th>
                              <th>BirthMonth</th>
                              <th>BirthDay</th>
                              <th>BirthYear</th>
                              <th>Cellphone Number</th>
                              <th>Phone Number</th>
                              <th>Address1</th>
                              <th>Address2</th>
                              <th></th>
                            </tr>
                          </thead>
                          <tbody>

                          </tbody>';


                    while($userData = mysqli_fetch_array($results)){
                        echo '<form action="customers.php" method="POST">';
                            echo '<tr>';
                                echo '<td>'.$userData['id'].'</td>';
                                echo '<td>'.$userData['Email'].'</td>';
                                echo '<td>'.$userData['Firstname'].'</td>';
                                echo '<td>'.$userData['Lastname'].'</td>';
                                echo '<td>'.$userData['Gender'].'</td>';
                                echo '<td>'.$userData['Titlename'].'</td>';
                                echo '<td>'.$userData['BirthMonth'].'</td>';
                                echo '<td>'.$userData['BirthDay'].'</td>';
                                echo '<td>'.$userData['BirthYear'].'</td>';
                                echo '<td>'.$userData['CellphoneNumber'].'</td>';
                                echo '<td>'.$userData['PhoneNumber'].'</td>';   
                                echo '<td>'.$userData['Address1'].'</td>';
                                echo '<td>'.$userData['Address2'].'</td>';

                                echo '<td><input type="hidden" name="hidden" value="'.$userData['id'].'">';
                                echo '<td><input type="submit" name="delete" value="Delete" class="btn btn-info" /  ></td>';
                            echo '</tr>';
                        echo '</form>';
                    }
                    echo '</table>';
    ?>

推荐答案

要做到这一点,您需要了解几件事.首先是安全位...

There are a few things you will need to know to be able to do this. Firstly the security bit...

基本上,您不希望信任提交给应用程序的数据.当接受要在MySQL语句中使用的数据时,您可以使用PHP内置的MySQL转义函数. 参考: http://php.net/manual/en/mysqli.real -escape-string.php

Basically you want to never trust data that is submitted to your application. When accepting data for use in a MySQL statement, you could use PHP's built in escape functions for MySQL. ref: http://php.net/manual/en/mysqli.real-escape-string.php

当您访问$ _POST超全局变量时,您似乎已经知道如何从提交的表单中获取数据.对于您的搜索,您将执行类似的操作.就像$ _POST ['search']之类的,您将从具有名称为"search"的文本元素的表单中发布数据.

You already seem to know how to get data from the submitted form as I see you accessed the $_POST superglobal. For your search, you will do something similar. It would be something like $_POST['search'] where you are getting data posted from a form with a text element with the name "search".

您将需要SQL查询来搜索结果.没有看到您的数据库架构,这对用户来说很难说,但是我怀疑这会奏效.

You will need an SQL query to search for results. Without seeing you database schema it's hard to say for user, but I suspect this would work.

$search = mysqli_real_escape_string($_POST['search']);
$query= "
    SELECT * 
    FROM customers 
    WHERE 
        Firstname LIKE '%{$search}%'
        OR Lastname LIKE '%{$search}%'
        OR Email LIKE '%{$search}%'
";
$results = mysqli_query($conn,$query);

一旦有了结果,就应该能够像给出的示例一样显示它们.

Once you have the results, you should be able to display them the same way you did with the example you gave.

希望这会有所帮助!

这篇关于如何使用PHP和Mysql在数据库表中放置搜索框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆