justlukeyou Posted March 26, 2011 Share Posted March 26, 2011 I have a script which queries a database and displays the relevant information. However, to query the database I currently have to manually edit the URL. Now I am trying to develop into a search form. On the homepage I have a form which enters into "searchterm". But I am but confused as to what I do with the "searchterm", can anyone advise how I can transfer the "searchterm" as the term which queries the database? <input type="text" name="searchterm" class="homepagesearch" > This is the code which queries and displays the query: ini_set('display_errors', 1); error_reporting(-1); $query = "SELECT * FROM questions"; if(isset($_GET['question'])) { /*query the database*/ $question = $_GET['question']; $query .= " WHERE question like '%$question%' LIMIT 0, 10"; } $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $question = $row['question']; $notes = $row['notes']; echo "$question $notes </br>"; } if ($_GET['question'] == $question ) { echo 'Sorry, this product is not available. Please visit our <a href="http://www.domain.co.uk">Homepage</a>.'; } Quote Link to comment Share on other sites More sharing options...
nethnet Posted March 26, 2011 Share Posted March 26, 2011 But I am but confused as to what I do with the "searchterm" Well, that depends what you are trying to do with 'searchterm'. What section of the database are you trying to search through? In general, a search query would look something like this: $query = mysql_query("SELECT * FROM `table_name` WHERE `column` LIKE '%{$_POST['searchterm']}%'"); `column` is whatever field you want to search for 'searchterm' in. You have a query similar to this already in your script using the $question variable as your search term, so are you trying to combine these two queries into one (a.k.a. find records that match BOTH of these requirements)? A little bit more detail about what exactly you are trying to search for and how you want to implement that with your pre-existing code would help. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted March 26, 2011 Author Share Posted March 26, 2011 Hi, I'm trying to create a search form which searches a database and displays it on the following page. Just like www.play.com or Amazon.com would use. 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.