使用Like运算符使用php在url中显示输入的文本

时间:2018-06-11 17:05:55

标签: php mysql sql

您已经通过在youtube中关注实现了搜索功能,但问题是每当我输入名称并单击搜索按钮时,它会在URL中显示输入的文本,如下所述。

    <?php session_start();



if(isset($_GET['search']))
{
    $valueToSearch=$_POST['valueToSearch'];
    $query="SELECT * FROM appointment WHERE 'first_name' LIKE '%".$valueToSearch."%'";
    $search=filtertable($query);

}
else{
    $query="SELECT * FROM appointment";
    $search_result=filtertable($query);
}

    function filterTable($query)
    {
        include 'includes/db.php';
        $filter_Result=mysqli_query($conn,$query);
            return $filter_Result;      
    }

  ?>

<form>

          <input name="valueToSearch" type="text"  placeholder="Search  by Name"/>

          <input type="submit" name="search" value="Search"/>

                <div class="body">
                    <div class="table-responsive">
                      <table class="table table-striped">
                        <thead>
                          <tr>
                            <th>#</th>
                            <th>Name</th>
                            <th>Gender</th>
                            <th>Department</th>
                            <th>Phonenumber</th>

                          </tr>
                        </thead>
                        <tbody>
                          <?php
                             while ($row=mysqli_fetch_array($search_result)):?>


                                    <tr>

                                        <td><?php echo $row['appoin_id'];?></td>

                                        <td><?php echo $row['first_name'];?> </td>

                                        <td><?php echo $row['gender'];?></td>

                                        <td><?php echo $row['department'];?></td>

                                        <td><?php echo $row['phone_no'];?></td>

                                    </tr>

                                <?php endwhile;?>
                        </tbody>
                      </table>
                    </div>
                </div>
                </form>

它在网址中显示如下

http://website.com/admin/search.php?valueToSearch=df&search=Search

在错误日志中显示错误为PHP注意:未定义的索引:第7行的admin / search.php中的valueToSearch。

1 个答案:

答案 0 :(得分:0)

<?php session_start();



if(isset($_POST['search']))
{
    $valueToSearch = $_POST['valueToSearch'];
    $query="SELECT * FROM appointment WHERE first_name LIKE '%".$valueToSearch."%'";        
    $search_result=filterTable($query);

}
else{
    $query="SELECT * FROM appointment";
    $search_result=filterTable($query);
    echo "No Records Found";
}

    function filterTable($query)
    {
        include 'includes/db.php';
        $filter_Result=mysqli_query($conn,$query);
            return $filter_Result;      
    }

&GT;