使用HTML表单搜索栏和PHP从数据库中搜索特定记录

时间:2017-09-18 06:44:26

标签: php html

我正在尝试使用html表单和php的搜索栏从数据库中搜索特定记录以进行后端查询。 这是我用于搜索的html表单代码:

<form style="float: right;margin-right: 80px;margin-top: 30px;" action="search_file.php" method="get">
            <input type="search" name="search">
           </form>

以下是我的搜索机制或流程的PHP代码:

<?php


              $search=$_GET['search'];
                $stores = "select * From uploadfile";
                $stores_sql = mysqli_query($conn, $stores);

                while($row1 = mysqli_fetch_array($stores_sql)){
                    $name = $row1['name'];
                    $city = $row1['city'];
                    $email = $row1['email'];
                    $phone = $row1['phone'];
                    $file = $row1['uploaded_file'];
                    $address=$row1['address'];

                    if($search==$name){
                    echo"<tr><td>$name</td><td>$city</td><td>$phone</td><td>$email</td><td><a href='uploads/$file' download>Download the file</a></td><td>$address</td></tr>";    
                    }
                    elseif($search==$city){
                        echo"<tr><td>$name</td><td>$city</td><td>$phone</td><td>$email</td><td><a href='uploads/$file' download>Download the file</a></td><td>$address</td></tr>";
                    }
                    elseif($search==$email){
                        echo"<tr><td>$name</td><td>$city</td><td>$phone</td><td>$email</td><td><a href='uploads/$file' download>Download the file</a></td><td>$address</td></tr>";
                    }
                    else{
                        echo "keep trying!!!";
                    }



                }
                ?> 

2 个答案:

答案 0 :(得分:1)

将您的查询更改为

"select * From uploadfile where name = '".$search."' or city = '".$search."' or email = '".$search."'";

或者您可以使用类似查询来获取与搜索字词类似的所有记录

"select * From uploadfile where name like('%".$search."%)' or city like('%".$search."%)' or email like('%".$search."%)'";

答案 1 :(得分:0)

使用此代码进行搜索:

$search=$_GET['search'];
$stores = "select * From uploadfile WHERE name = '".$search."' OR city = '".$serach."' OR email ='".$serach."'";
$stores_sql = mysqli_query($conn, $stores);