imperium2335 Posted March 13, 2009 Share Posted March 13, 2009 Hi, this is my first post, I am slightly above noob in PHP so please be merciful. I am trying to create a search facility where the user can type in a word, and/or specify other things from a series of drop down lists (there are 5 drop down lists as well as the word field). I have got it to return all the records in the Mysql database that have the word in field. But I am stumped when it comes to the following: How do I get it to take into account other selections from the drop down lists IF specified by the user (the default option is "Dont Specify"), btw the word field doesn't need to be filled in either as long as at least on of the selections from a drop down box is made. Second, how do i get it to match a word in the form field to a range of words in a record in the database, e.g. a record may have the words "chocolate,banana,orange", I would want that record returned if the user just typed "chocolate". Below is a bit of my code, thanks in advance for your help. <?PHP if (!file_exists("dbconnect.php")) { die("Database settings not found, administrator intervention required.") ; } else { require("dbconnect.php") ; //Must connect to the database. } $word = $_REQUEST['word'] ; //Variables from the form. $type = $_REQUEST['type'] ; $flavor = $_REQUEST['flavor'] ; $tiers = $_REQUEST['tiers'] ; $serves = $_REQUEST['serves'] ; $price = $_REQUEST['price'] ; $def = "Dont Specify" ; if(!isset($word)) { unset($word) ; } if($type == $def) { unset($type) ; //$type = NULL ; } if($flavor == $def) { unset($flavor) ; //$tiers = NULL ; } if($serves == $def) { unset($serves) ; //$serves = NULL ; } if($price == $def) { unset($price) ; //$price = NULL ; } if(isset($word)) { $query = "SELECT * FROM image_bank WHERE tags LIKE '%$word%'" ; } $result = mysql_query($query) ; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $dbtags = $row['tags'] ; $dbtiers = $row['tiers'] ; $dbtype = $row['type'] ; $dbflavor = $row['flavor'] ; $dbserves = $row['serves'] ; $dbprice = $row['price'] ; $imgurl = $row['url']; $thumburl = $row['thumb_url'] ; echo "$dbtags" . "<br />" ; echo "$dbtiers" . "<br />" ; echo "$dbtype" . "<br />" ; echo "$dbflavor" . "<br />" ; echo "$dbserves" . "<br />" ; echo "$dbprice" . "<br />" ; echo '<a href="' . "$imgurl" . '">' . "$imgurl" . "</a>" . "<br />" ; echo '<img src="' . "$thumburl" . '" height="100" width="50" />' ; } include("dbdisconnect.php") ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/149197-php-search-with-mysql-multiple-form-variables/ Share on other sites More sharing options...
JonnoTheDev Posted March 13, 2009 Share Posted March 13, 2009 You need to build up the sql based on the form fields entered. So you start of with the basic sql (also escape any user input prior to running a query): $word = mysql_real_escape_string($_REQUEST['word']); $sql = "SELECT * FROM image_bank WHERE tags LIKE '%".$word%."'"; Then check the additional fields and add to the query if(strlen($_REQUEST['field2'])) { $field2 = mysql_real_escape_string($_REQUEST['field2']); $sql .= " AND tags LIKE '%".$field2%."'"; } Then run the query mysql_query($sql); Quote Link to comment https://forums.phpfreaks.com/topic/149197-php-search-with-mysql-multiple-form-variables/#findComment-783687 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.