Re: Probably an easy question - blank search fields (PHP & MySQL)

From: Date: Sun, 06 Jan 2002 14:22:53 +0000
Subject: Re: Probably an easy question - blank search fields (PHP & MySQL)
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-15451@lists.php.net to get a copy of this message
$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); Gary Smith wrote: > 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 > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net > For additional commands, e-mail: php-db-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.db (#15451) next »