Re: The most bizarre problem I've ever encountered: text box and mysql_close()
| From: | Daniel Convissor | Date: | Wed, 27 Dec 2000 07:20:29 +0000 |
| Subject: | Re: The most bizarre problem I've ever encountered: text box and mysql_close() | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-31930@lists.php.net to get a copy of this message | ||
Hi Daniel:
I think the problem you were running into was because of improper nesting and
table formatting. I suggest using more white space to clarify your nesting.
Anyway, I trust this untested code will be of assistance...
<form action="book_search.php" method="get">
<select name="search_option">
<option>Title
<option>Author
</select>
<!--this is the text box-->
<input type="text" name="search_field">
<input type="submit" name="submit" value="Search">
</form>
<?
if($submit){
include "incs/dbvars.inc";
# Using addslashes rather than ereg_replace...
# also doing rawurldecode incase submitted by hyperlink
$search_field=addslashes(rawurldecode(trim($search_field)));
$query = "SELECT user.alias, user.email, book.title, " .
"book.author, book.genre, book.book_id as bid " .
"FROM user, book WHERE book.keeper_email IS NULL ";
# Using switch rather than series of if statements...
switch ($search_option) {
case "title":
$query .= "AND book.title LIKE '%$search_field%' ";
# NOTE: use of & for XHTML compliance and rawurlencode()!
$links="search=1&search_option=title&search_field=";
$links .= rawurlencode($search_field);
break;
case "author":
$query .= "AND book.author LIKE '%$search_field%' ";
# Switched "title" to "author"...
$links="search=1&search_option=author&search_field=";
$links .= rawurlencode($search_field);
break;
case "genre":
# NOT QUITE SURE WHY YOU USED "elseif($genre)"
# AND "'$genre'" rather than "$search_field"
# so did some modifications here...
$query .= "AND book.genre = '$genre' ";
$links="search=1&search_option=genre&genre=";
$links .= rawurlencode($genre);
break;
default:
$links="search=1";
}
# Move display base and pages to here:
# 1) Lowers memory requirements due to smaller
# record set being returned by query.
# 2) Reduces if statements that need to be evaluated
# while displaying the record set.
$display_number = 10;
if(!$page) $page = 1;
$display_base = ($page-1)*$display_number;
$query .= "AND user.user_id = book.user_id " .
"ORDER BY author, title LIMIT $display_base, $display_number";
$result = @mysql_query($query,$connID);
$num_rows = @mysql_num_rows($connID);
if($num_rows == 0) {
echo "No matches were found.\n";
} else {
echo "<table>\n";
# Using while statement rather than if and for statements...
# Also reducing memory requirements by doing MYSQL_ASSOC...
while ( $book = mysql_fetch_array($result, MYSQL_ASSOC) ) {
echo " <tr>\n";
# Need to move arrays out of quoted section
# and put quotes around index key names.
echo " <td>" . $book['title'] . "</td>\n";
echo " <td>" . $book['author'] . "</td>\n";
echo " <td>" . $book['genre'] . "</td>\n";
echo " <td><a href=\"mailto:";
echo $book['address'] . "\">" . $book['alias'];
echo "</a></td>\n <td><a href=\"checkout.php?";
echo "bid=" . rawurlencode($book['bid']);
echo "&book_title=" . rawurlencode($book['title']);
echo "&author=" . rawurlencode($book['author']);
echo "&user_email=" . rawurlencode($book['email']);
echo "&alias=" . rawurlencode($book['alias']);
echo "\">check out</a></td>\n </tr>\n";
} # End of while fetch array.
echo " <tr><td align=\"center\" colspan=\"5\">";
if ($page > 1) {
$back=$page-1;
echo "<a href=\"book_search.php?";
echo "$links&page=$back\">Back</a>\n";
}
# Not sure of the vars/numbers in this if statement.
# You may need to tweak this...
if($num_rows - $display_base > $display_number){
$next=$page+1;
echo "<a href=\"book_search.php?";
echo "$links&page=$next\">Next</a>\n";
}
echo " </td></tr>\n </table>\n";
} # End of if num rows
mysql_close($connID);
} # End of if submit
Good luck,
--Dan
--
PHP scripts that make your job easier
http://www.analysisandsolutions.com/code/
SQL Solution | Layout Solution | Form Solution
T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y
4015 7 Ave, Brooklyn NY 11232 v: 718-854-0335 f: 718-854-0409