[thelist] GET POST Form PHP

Kae Verens kverens at contactjuggling.org
Mon Feb 24 05:51:01 CST 2003


Andrew Maynes wrote:
> PHP guys and dolls
>
> Could someone clarify my muddy thoughts regarding using a form to search a MySQL
> DB with php?
>
> I am need to either post or get a query and wondered if any of you can tell me
> what the best way to proceed is?
>
> Andrew
>

A simple search form might be simply this:
<form action="search.php">
  <label>Search Term: <input type="text" name="s" /></label><br />
  <input type="submit" value="Search" />
</form>

This would send the search term to search.php, which may contain
something like this:
$q=mysql_query('select id,name from searchtable where desc like
"%'.$s.'%" or name like "%'.$s.'%" limit 0,20');
if(mysql_num_rows($q)){
  ?>Items matching your search:<ul><?
  while($r=mysql_fetch_array($q)){
   ?><li><a
href="showitems.php?id=<?=$r['id'];?>"><?=htmlspecialchars($r['name']);?></a></li><?
  }
  ?></ul><?
}else{
  ?><em>No matches for that search</em><?
}

Assumes you are searching a table called "searchtable", which contains
fields called id, name, and desc.

--
Kae Verens               _______\_   webworks.ie
work: www.webworks.ie       _____\\__   webhosts
play: www.contactjuggling.org  ___\\\___  design
kae:  kverens.contactjuggling.org _\\\\____ code




More information about the thelist mailing list