Page 1 of 1

Problem searching database from three seperate form entries

Posted: Mon Jun 25, 2007 3:12 pm
by seth_web
Hi there! I'm having trouble figuring this out. Any help would be greatly appreciated.

I have a form that has three text input boxes. I would like for them to each search a separate column in the same database when the user hits the submit button. For example, the first field in the form is "author" so the user would put in an author's name like "Spurgeon." The user could also put a topic into the "topic" search box like "faith." When the user submits the submit button I would like to to display matching records from only the "author" and "topic" columns in the database. In the case of this example only records containing "spurgeon" from "authors" and "faith" from "topics" would be displayed.

Anybody have an idea on the best way to go about this?

Posted: Mon Jun 25, 2007 4:46 pm
by Gente

Code: Select all

$sql = 'SELECT * FROM table WHERE 1 = 1';
if (isset($_POST['field1']))
{
  $sql .= " AND field1 LIKE '%".$_POST['field1']."%'";
}
if (isset($_POST['field2']))
{
  $sql .= " AND field2 LIKE '%".$_POST['field2']."%'";
}
if (isset($_POST['field3']))
{
  $sql .= " AND field3 LIKE '%".$_POST['field3']."%'";
}
...
Is this what you want? :?

Posted: Mon Jun 25, 2007 6:10 pm
by ReverendDexter
I would ammend those from

Code: Select all

if (isset($_POST['field1'])) 
{...}
to

Code: Select all

if (!empty($_POST['field1'])) 
{...}