soycharliente Posted December 3, 2007 Share Posted December 3, 2007 I am having a problem with some radio buttons that I've placed on a search form. I think that my logic in checking which radio has been selected is wrong, therefore I'm getting incorrect results. It's a search form to search a db of books for a library. There is a page that lists all the categories of books (physics, calculus, chemistry, etc.). The search form is reached after choosing a category. So in the form it already tells you what book category you are searching in. The form has an input for the text and 4 radio options (all, isbn, title OR author, title AND author). When I chose all, it displays results for all book categories instead of just the book category that it's in. Any help/ideas? Here's all the code: <?php require("main.php"); session_start(); ?> <html> <head> <title>Books Management</title> </head> <body> <fieldset> <legend><strong>Search Books in Book Category</strong></legend> <strong>Book Category ID:</strong> <?php echo $_GET["id"]; ?><br /> <strong>Book Category Name:</strong> <?php echo getCategoryName($_GET["id"]); ?> <form action="managebooks.php?id=<?php echo $_GET["id"]; ?>" method="post"> <p>Search Books By: <input type="radio" name="searchbooksby" value="all" checked="checked" /> All <input type="radio" name="searchbooksby" value="isbn" /> ISBN <input type="radio" name="searchbooksby" value="tora" /> Title OR Author <input type="radio" name="searchbooksby" value="tanda" /> Title AND Author</p> <p><input type="text" name="searchbooksdata" value="" size="40" maxlength="255" /></p> <input type="hidden" name="searchbookscategoryid" value="<?php echo $_GET["id"]; ?>" /> <p><input type="submit" name="searchbookssubmit" value="Search" /> <input type="submit" name="searchallbookssubmit" value="Search All Categories" /></p> </form> </fieldset> <fieldset> <legend><strong>Search Results</strong></legend> <p><?php print_r($_POST); ?></p> <?php if (isset($_POST["searchbookssubmit"])) { $searchbooksdata = $_POST["searchbooksdata"]; $searchbooksby = $_POST["searchbooksby"]; $searchbookscategoryid = $_POST["searchbookscategoryid"]; dbconnect(); $query = "SELECT * FROM books WHERE "; if ($searchbooksby === "isbn") { $query .= "isbn LIKE '%$searchbooksdata%'"; } else if ($searchbooksby === "tora") { $query .= "title LIKE '%$searchbooksdata%' OR author LIKE '%$searchbooksdata%'"; } else if ($searchbooksby === "tanda") { $query .= "title LIKE '%$searchbooksdata%' AND author LIKE '%$searchbooksdata%'"; } else { $query .= "isbn LIKE '%$searchbooksdata%' OR title LIKE '%$searchbooksdata%' OR author LIKE '%$searchbooksdata%'"; } $query .= " AND categoryid='$searchbookscategoryid'"; $result = mysql_query($query) OR DIE ("Error searching for books."); if (mysql_num_rows($result) > 0) { echo '<table border="0" cellpadding="3" cellspacing="2" style="border: 1px solid black;">'; echo '<tr style="text-align: left;"><th>ISBN</th><th>Title</th><th>Author</th><th>Book Category</th></tr>'; while ($row = mysql_fetch_array($result)) { $id = $row["id"]; $isbn = $row["isbn"]; $author = $row["author"]; $title = $row["title"]; $categoryname = getCategoryName($row["categoryid"]); echo '<tr><td>'.$isbn.'</td>'; echo '<td>'.myWrap($title, 40, '<br />').'</td>'; echo '<td>'.$author.'</td>'; echo '<td>'.$categoryname.'</td>'; echo '<td><a href="editbooks.php?id='.$id.'">Edit</a></td>'; echo '</tr>'; } echo '</table>'; } else { echo '<p><span style="color: red;">No matches.</span></p>'; } dbclose(); } else if (isset($_POST["searchallbookssubmit"])) { $searchbooksdata = $_POST["searchbooksdata"]; $searchbooksby = $_POST["searchbooksby"]; $searchbookscategoryid = $_POST["searchbookscategoryid"]; dbconnect(); $query = "SELECT * FROM books WHERE "; if ($searchbooksby === "isbn") { $query .= "isbn LIKE '%$searchbooksdata%'"; } else if ($searchbooksby === "tora") { $query .= "title LIKE '%$searchbooksdata%' OR author LIKE '%$searchbooksdata%'"; } else if ($searchbooksby === "tanda") { $query .= "title LIKE '%$searchbooksdata%' AND author LIKE '%$searchbooksdata%'"; } else { $query .= "isbn LIKE '%$searchbooksdata%' OR title LIKE '%$searchbooksdata%' OR author LIKE '%$searchbooksdata%'"; } $result = mysql_query($query) OR DIE ("Error searching for books."); if (mysql_num_rows($result) > 0) { echo '<table border="0" cellpadding="3" cellspacing="2" style="border: 1px solid black;">'; echo '<tr style="text-align: left;"><th>ISBN</th><th>Title</th><th>Author</th><th>Book Category</th></tr>'; while ($row = mysql_fetch_array($result)) { $id = $row["id"]; $isbn = $row["isbn"]; $author = $row["author"]; $title = $row["title"]; $categoryname = getCategoryName($row["categoryid"]); echo '<tr><td>'.$isbn.'</td>'; echo '<td>'.myWrap($title, 40, '<br />').'</td>'; echo '<td>'.$author.'</td>'; echo '<td>'.$categoryname.'</td>'; echo '<td><a href="editbooks.php?id='.$id.'">Edit</a></td>'; echo '</tr>'; } echo '</table>'; } else { echo '<p><span style="color: red;">No matches.</span></p>'; } dbclose(); } ?> </fieldset> </body> </html> DB stucture: --books-- id categoryid isbn type author title edition desc copies --categories-- id name desc Quote Link to comment Share on other sites More sharing options...
soycharliente Posted December 3, 2007 Author Share Posted December 3, 2007 *bump* Quote Link to comment Share on other sites More sharing options...
soycharliente Posted December 3, 2007 Author Share Posted December 3, 2007 I think I figured it out. I just change the corresponding part to this: <?php $query = "SELECT * FROM books WHERE "; if ($searchbooksby === "isbn") { $query .= "isbn LIKE '%$searchbooksdata%' AND "; } else if ($searchbooksby === "tora") { $query .= "title LIKE '%$searchbooksdata%' OR author LIKE '%$searchbooksdata%' AND "; } else if ($searchbooksby === "tanda") { $query .= "title LIKE '%$searchbooksdata%' AND author LIKE '%$searchbooksdata%' AND "; } $query .= "categoryid='$searchbookscategoryid'"; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.