Probably an easy question - blank search fields (PHP & MySQL)
| From: | Gary Smith | Date: | Sun, 06 Jan 2002 11:18:55 +0000 |
| Subject: | Probably an easy question - blank search fields (PHP & MySQL) | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-15448@lists.php.net to get a copy of this message | ||
Hiya, I'm writing a backend in MySQL with a PHP frontend. One of the PHP
pages searches the database. The user can enter as many or as few fields as
they like. I then search the database using this code:
$result = mysql_query("select * from equipment where (category='$category')
and (description like '$description') and (roomloc='$roomloc') and
(quantity like '$quantity') and (serialno like '$serialno') and (patdue
like '$patdue')",$db);
Messy, I know. The category and roomloc are <option>s and therefore are
definite. However, if the user enters nothing for the other fields, they
get given no results. If I set the default value of each field to % in the
page that calls this, then it works fine.
It's just not what I'd like to do - have the user clear out % from each
field before putting their data in. Is there an easy way to code "if the
field is blank, send %, else send the users entry" or just putting % before
whatever is sent? (this shouldn't mess up what's being entered, surely?)
Thanks for any help.
--
Gary Smith
E: shadyron@l33t-d00d.co.uk
W: http://www.l33t-d00d.co.uk
Pics: http://photos.l33t-d00d.co.uk