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?
Problem searching database from three seperate form entries
Moderator: General Moderators
- Gente
- Forum Contributor
- Posts: 252
- Joined: Wed Jun 13, 2007 9:43 am
- Location: Ukraine, Kharkov
- Contact:
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']."%'";
}
...- ReverendDexter
- Forum Contributor
- Posts: 193
- Joined: Tue May 29, 2007 1:26 pm
- Location: Chico, CA
I would ammend those from
to
Code: Select all
if (isset($_POST['field1']))
{...}Code: Select all
if (!empty($_POST['field1']))
{...}