0

My table has 4 columns. "isbn, author, title, price". I want to search one of them combining all 4 fields. Like: if author "kayle" write 4 books with same price ($50) but with different title, for that in search page, if i select author:kayle and hit search, then it shows all books with all different or may be same prices and titles. Now i want to select author:kayle and price:$50 at the same time, and hit enter. For that it will show only $50 prices books wrote by kayle and it will appear in the table with 4 rows.

I try to combine it but stuck with the second step searching query. Here is my code if any one understand what i want to do, please share it. Here is my code:

<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>";
        }
    }
}
?>

Thanks in advance.

4

1 回答 1

1

尝试这个

首次搜索

   select* from table where author="kayle";

第二次搜索

   select * from table where author="kayle" and price="50";
于 2012-07-09T12:21:43.917 回答