使用关键字和多个下拉列表搜索功能

时间:2016-10-23 05:26:23

标签: php html mysql search filter

我试图在我的网站上使用关键字和下拉列表制作搜索功能,我一直在谷歌搜索,但我似乎无法找到相同的代码。我尝试将我找到的代码组合在一起,然后我就达成了这个目标。是的,只有在3下拉菜单中选择一个选项时,它才有效。我的问题是,如果我只在一个下拉列表中选择一个选项并继续搜索,它就无法工作。

<!DOCTYPE html>
<html>
<head>
    <title>Activity</title>
    <link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="screen">


    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="css/bootstrap-responsive.css" rel="stylesheet">
</head>
<body>
    <?php
        include('dbcon.php');
    ?>

    <div class="container">
        <div class="row-fluid">
            <div class="span12">
                        <br />
                        <br />
                <div class="row-fluid">
                    <div class="span6">
                        <div class="span6">
                            <form method="POST" action="search.php" class="navbar-search pull-left">
                                <input type="text" name="search" class="search-query" placeholder="Search">
<select name="author" class="input">
    <option value="all">Select Author</option>
    <option value="bbb">bbb</option>
    <option value="rrr">rrr</option>
    <option value="ooo">ooo</option>                                                    
    <option value="hhh">hhh</option>
    <option value="KingKoy Max">KingKoy Max</option>
</select>
<select name="category" class="input">
    <option value="all">Select Category</option>
    <option value="ccc">ccc</option>
    <option value="ttt">ttt</option>
    <option value="iii">iii</option>
    <option value="ggg">ggg</option>
    <option value="Romance">Romance</option>    
</select>
<select name="year_publication" class="input">
    <option value="all">Select Year</option>
    <option value="2014">2014</option>
    <option value="2015">2015</option>
    <option value="2016">2016</option>
</select>
        <input type="submit" value="Search Now">
                            </form>
                        </div>
                        <br />
                        <br />
                        <br />
                        <table class="table table-bordered  table-hover table-striped" style="width:800px;"> 
                            <thead>
                                <tr>
                                    <th>Title</th>
                                    <th>Author</th>
                                    <th>Category</th>
                                    <th>Year Publication</th>
                                    <th>Place Publication</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                    $query=mysql_query("select * from book")or die(mysql_error());
                                    while($row=mysql_fetch_array($query)){
                                    ?>

                                    <tr>
                                        <td><?php echo $row['title']; ?></td>
                                        <td><?php echo $row['author']; ?></td>
                                        <td><?php echo $row['category']; ?></td>
                                        <td><?php echo $row['year_publication']; ?></td>
                                        <td><?php echo $row['place_publication']; ?></td>
                                    </tr>
                                    <?php
                                    }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

</body>

这是php代码:

    <?php
        include('dbcon.php');
    ?>
                                <?php



                                    $search=$_POST['search'];
                                    $author=$_POST['author'];
                                    $category=$_POST['category'];
                                    $year_publication=$_POST['year_publication'];
                                    $query=mysql_query("select * from book where 
                                    title like '%$search%' AND author like '%$author%' AND category like '%$category%' AND year_publication like '%$year_publication%' ")or die(mysql_error());


                                    while($row=mysql_fetch_array($query)){
                                    ?>

                                    <tr>
                                        <td><?php echo $row['title']; ?></td>
                                        <td><?php echo $row['author']; ?></td>
                                        <td><?php echo $row['category']; ?></td>
                                        <td><?php echo $row['year_publication']; ?></td>
                                        <td><?php echo $row['place_publication']; ?></td>

                                    </tr>
                                    <?php
                                    }
                                    ?>

1 个答案:

答案 0 :(得分:0)

您可以检查是否选择了下拉列表以及空集空白变量。尝试替换

$search=$_POST['search'];
$author=$_POST['author'];
$category=$_POST['category'];
$year_publication=$_POST['year_publication'];

通过

 if(empty($_POST['search']))
   $search = '';
 else
   $search=$_POST['search'];
 if(empty($_POST['author']))
   $author = '';
 else
   $author=$_POST['author'];
 if(empty($_POST['category']))
   $category = '';
 else
   $category=$_POST['category'];
 if(empty($_POST['year_publication']))
   $year_publication = '';
 else
   $year_publication=$_POST['year_publication'];