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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write the WHERE clause?

Status
Not open for further replies.

seahorse123

Programmer
Jul 25, 2005
39
US
I want to submit a form, which has multiple dropdown menu: dd1, dd2,dd3.
these values will be passed to the process page to populate the data from DB, on the process page I have code:

Code:
$v_dd1=$_request['dd1'];
$v_dd2=$_request['dd2'];
$v_dd3=$_request['dd3'];

if(($v_dd1)<>'') {$clause = "field1=$v_dd1";}
if(($v_dd2)<>'') {$clause .= "and field2=$v_dd2";}
if(($v_dd3)<>'') {$clause .= "and field3=$v_dd3";}

select * from table where $clause;

Now the problem is:
On submit form, user can select one or more dropdown menu, but on process page, how to write the correct WHERE clause? Right now, if user select "dd1", then SELECT statement works fine("select * from table where field1=$v_dd1), but if user only select "dd2", then SELECT statement will be wrong("select * from table where and field2=$v_dd2"), there is an extra "and" in the statement.

anyone can help me? thanks

michael
 
Use this:

$clause_array=array();

if(($v_dd1)<>'') {$clause_array[] = "field1=$v_dd1";}
if(($v_dd2)<>'') {$clause_array[] = "field2=$v_dd2";}
if(($v_dd3)<>'') {$clause_array[] = "field3=$v_dd3";}
$clause = implode (" AND ",$clause_array);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top