Jump to content

Archived

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

simon0304

search boxes in dremaweaver

Recommended Posts

Hi all,

I have a database for insect species. I have set up a search box which a user can enter a keyword and it pull out any info from the database containing that word. I have two problems. Firstly Everything works ok, but I have just noticed that if no entry is place in the text box and the search button is pressed then everything from the database is displayed. Is there anyway to correct this.

 

the script is as follows

 

SELECT ID, BF, Vernacular, Taxon

FROM species

WHERE Vernacular LIKE '%colname%'

ORDER BY Vernacular ASC

 

 

Hope you can help

Thanks

Simon

Share this post


Link to post
Share on other sites
code and stuff

Try this, assuming you can change the variables accordingly.

 

// variable for the $_POST
$search_string = $_POST['searchfield'];

if ($search_string == "") // asking if its empty
{
echo "Sorry, please <a href=javascript:history.go(-1);>go back</a> and enter something in the search field!";
}

elseif ($search_string != "") // if its not empty, you an also use if (!empty($search_string))
{
$query = "SELECT ID, BF, Vernacular, Taxon FROM species WHERE Vernacular LIKE '%colname%' ORDER BY Vernacular ASC";
$result = mysql_query($query);
$result_total = mysql_num_rows($result); // see how many results were pulled
if ($result_total == 0) // if nothing is returned in the search do this
{
echo "Sorry, no results matched your search criteria.";
}
if ($result_total > 0) // if results are actually brought up do this
{
echo "Congratulations! There were $result_total results found matching the criteria you searched for!";
// give loop to show all the results here
}
else // if something other than that goes wrong
{
echo "There was a problem outside the magical realm of my knowledge, try again.";
}
}

mysql_close(); // close connections

 

Try that, sorry if its wrong in some ways, I'm studying for my History exam.

Share this post


Link to post
Share on other sites

×

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.