Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search using php

Status
Not open for further replies.

eja2000

Programmer
Joined
Nov 30, 2003
Messages
209
Location
NG
i have a form with 2 search boxes..i want the user to either enter data into one or both of the txtboxes..what sql statement can i use that will work even if i enter data into just one of the boxes?
thx
 
you should see if one or another text input has value on it in order to construct the WHERE statment, so:

if $text1 has data
then WHERE field1 like $text1

if $text2 has data
then WHERE.= AND field1 like $text1

comprendez vouz?

could be better ways anyway.
 
I don't think you are asking the right question. It's not a matter of universal SQL statement you can use -- it's a matter of what program logic you can use to detect populated inputs and only include those values in the query.

Something like:

Code:
<?php
$query = "SELECT somecolumn from sometable";
$where_in_query = FALSE;

if (strlen($_POST['input_name_one']) != 0)
{
	$query .= " WHERE";
	$query .= " searchcolumn1 = '" . $_POST['input_name_one'] . "'";
	$where_in_query = TRUE;
	$number_of_where_clauses++;
}

if (strlen($_POST['input_name_two']) != 0)
{
	if ($where_in_query == TRUE)
	{
		$query .= " AND";
	}
	else
	{
		$query .= " WHERE";
	}
	
	$query .= " searchcolumn2 = '" . $_POST['input_name_two'] . "'";
}
?>

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top