kingnutter Posted July 31, 2009 Share Posted July 31, 2009 Hi everyone. This is my first attempt at Search form. It works for a single search term, but my OPTION VALUE 'All' throws up errors (a common problem I can see from Googling, but no solution is working for me). Can anyone tell me where I might be going wrong? Many thanks, KN <form name="search" method="post" action="index.php"> Search for: <input type="text" name="find" /> in <Select NAME="field"> <Option VALUE="track_artist">Track Artist</option> <Option VALUE="track_title">Track Title</option> <Option VALUE="track_title, track_artist">All</option> </Select> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> <?php // includes include('conf.php'); include('functions.php'); // Grab POST data sent from form $field = $_POST['field'] ; $find = $_POST['find'] ; $searching = $_POST['searching'] ; //This is only displayed if they have submitted the form if ($searching =="yes") { echo "<h2>Results</h2><p>"; //If they did not enter a search term we give them an error if ($find == "") { echo "<p>You forgot to enter a search term"; exit; } // Otherwise we connect to our Database // open database connection $connection = mysql_connect($host, $user, $pass) or die ('unable to connect!'); //select database mysql_select_db($db) or die ('unable to select database!'); // We preform a bit of filtering $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); if ($field=='All') { $data = mysql_query("SELECT track_artist, track_title FROM tracks WHERE upper(track_artist) LIKE '%$find%' OR upper(track_title) LIKE '%$find%'"); } else //Or we search for our single search term, in the field the user specified { $data = mysql_query("SELECT track_artist, track_title FROM tracks WHERE upper($field) LIKE'%$find%'"); } // And we display the results while($result = mysql_fetch_array( $data )) { echo $result['track_artist']; echo " "; echo $result['track_title']; echo "<br>"; echo "<br>"; } //This counts the number or results - and if there wasn't any it gives them a little message explaining that $anymatches=mysql_num_rows($data); if ($anymatches == 0) { echo "Sorry, but we can not find an entry to match your query<br><br>"; } //And we remind them what they searched for echo "<b>Searched For:</b> " .$find; } else { ?> Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted July 31, 2009 Share Posted July 31, 2009 first problem is that $field will never == 'ALL' because the value you are sending in the form is "track_title, track_artist" what exactly is the error you are getting here ? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 31, 2009 Share Posted July 31, 2009 The value in $field is the portion of the option-tag within value="". Change: if ($field=='All') To: echo $field . '<br />'; if ($field=='All') And then you'll see why $field is not equal to 'All'. Quote Link to comment Share on other sites More sharing options...
kingnutter Posted July 31, 2009 Author Share Posted July 31, 2009 Duh. Not even a php problem. Sorry guys. I've changed the Option values: <Option VALUE="track_artist">Track Artist</option> <Option VALUE="track_title">Track Title</option> <Option VALUE="All">All</option> and the page is just refreshing. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 31, 2009 Share Posted July 31, 2009 Double check your queries (i.e. echo them out) and add some calls to mysql_error() to try and get more information about why it's not working. 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.