搜索多个类别和标准

时间:2012-07-09 12:17:16

标签: php mysql

我的表有4列。 “isbn,作者,头衔,价格”。 我想搜索其中一个结合所有4个字段。 喜欢:如果作者“kayle”以相同的价格(50美元)写了4本书但标题不同,那么在搜索页面中,如果我选择作者:kayle并点击搜索,那么它会显示所有不同的书籍或者可能是相同的价格和标题。现在我想选择作者:kayle和价格:同时50美元,并点击回车。为此,它将只显示由kayle写的50美元的价格书,它将出现在4行的表格中。

我尝试将它结合起来,但坚持第二步搜索查询。这是我的代码,如果有人理解我想做什么,请分享。这是我的代码:

<form method="post" action="search_form.php">
<input type="hidden" name="submitted" value="true"/>
<table border="1">
<tr>
    <td style="padding:3px 10px; font-weight:bold;">ISBN</td>
    <td style="padding:3px;">
    <input type="hidden" name="category_isbn" id="category_isbn" value="isbn"/>
    : <select name='criteria_isbn' id="criteria_isbn" onchange="ajaxFunction()">
        <option selected="selected">- Select -</option>
    <?php
        $order = "SELECT isbn FROM books" or die (mysql_error());
        $result = mysql_query($order);  
        while($data = mysql_fetch_array($result))
        {
         echo ("<option> $data[isbn] </option>");
        }
    ?>
    </select>
    </td>
    <td style="padding:3px 10px; font-weight:bold;">Author</td>
    <td style="padding:3px;">
    <input type="hidden" name="category_author" id="category_author" value="author"/>
    : <select name='criteria_author' id="criteria_author" onchange="ajaxFunction()">
        <option selected="selected">- Select -</option>
    <?php
        $order = "SELECT author FROM books" or die (mysql_error());
        $result = mysql_query($order);  
        while($data = mysql_fetch_array($result))
        {
         echo ("<option> $data[author] </option>");
        }
    ?>
    </select>
    </td>
    <td style="padding:3px 10px; font-weight:bold;">Title</td>
    <td style="padding:3px;">
    <input type="hidden" name="category_title" id="category_title" value="title"/>
    : <select name='criteria_title' id="criteria_title" onchange="ajaxFunction()">
        <option selected="selected">- Select -</option>
    <?php
        $order = "SELECT title FROM books" or die (mysql_error());
        $result = mysql_query($order);  
        while($data = mysql_fetch_array($result))
        {
         echo ("<option> $data[title] </option>");
        }
    ?>
    </select>
    </td>
    <td><input type="submit" /></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submitted']))
{
    $category_isbn = $_POST['category_isbn'];
    $criteria_isbn = $_POST['criteria_isbn'];
    $query = "SELECT * FROM books WHERE $category_isbn LIKE '%".$criteria_isbn."%'";
    $result = mysql_query($query) or die(mysql_error());
    $num_rows = mysql_num_rows($result);

    if(isset($_POST['criteria_isbn']))
    {
        $category_author = $_POST['category_author'];
        $criteria_author = $_POST['criteria_author'];
        $query = "SELECT * FROM books WHERE $category_author LIKE '%".$criteria_author."%'";
        $result = mysql_query($query) or die(mysql_error());
        $num_rows = mysql_num_rows($result);

        if(isset($_POST['criteria_author']))
        {
            $category_title = $_POST['category_title'];
            $criteria_title = $_POST['criteria_title'];
            $query = "SELECT * FROM books WHERE $category_title LIKE '%".$criteria_title."%'";
            $result = mysql_query($query) or die(mysql_error());
            $num_rows = mysql_num_rows($result);

            echo "$num_rows results found";
            echo "<table border= 1>";
            echo "<tr> <th>ISBN</th> <th>AUTHOR</th> <th>TITLE</th> <th>PRICE</th> </tr>";
                while($row = mysql_fetch_array($result))
                {
                    echo "<tr><td>";
                    echo $row['isbn'];
                    echo "</td><td>";
                    echo $row['author'];
                    echo "</td><td>";
                    echo $row['title'];
                    echo "</td><td>";
                    echo $row['price'];
                    echo "</td></tr>";
                }
            echo "</table>";
        }
    }
}
?>

提前致谢。

1 个答案:

答案 0 :(得分:1)

试试这个

第一次搜索

   select* from table where author="kayle";

进行第二次搜索

   select * from table where author="kayle" and price="50";