Jump to content

Search Form "searchterm"


justlukeyou

Recommended Posts

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>.';
}

Link to comment
https://forums.phpfreaks.com/topic/231783-search-form-searchterm/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.