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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.