Forums

This topic is locked

Search with multiple fields

Posted 04 Oct 2002 18:07:39
1
has voted
04 Oct 2002 18:07:39 Saeed Abdi posted:
How can I pass a query to a database from a form with two fields, one filed is text field and the other is a dropdown with column name which should tell the db which column to search.

For example lets say someone wants to search for john in the names column, what they would do is type the name in the text field and select the Username from the dropdown field.

Info
PHP with Mysql on Apache web server

Thanks in advance guys<img src=../images/dmxzone/forum/icon_smile_big.gif border=0 align=middle>

Replies

Replied 04 Oct 2002 18:41:37
04 Oct 2002 18:41:37 Brent Colflesh replied:
Dear Xlarge,
Pass both variables to the next page & create a dynamic SQL query w/them, ie:

SELECT *
FROM strVar1
WHERE SomeField LIKE strVar2

Regards,
Brent

Replied 04 Oct 2002 20:00:16
04 Oct 2002 20:00:16 Ned Frankly replied:
Brent's technique is basically correct, but his example uses a user-defined Table, not a column, and it will fail for a missing column (unless the search field accidentally matches a column name). A little more explicit example that should work:

SELECT *
FROM TableName
WHERE strVar1 LIKE '%strVar2%'

Variable Definitions: (name, default, runtime):
strVar1 TheDefaultColumnName Request.Form("searchcolumn"
strVar2 xyz Request.Form("search"

Defining strVar1 with a legitimate column name (TheDefaultColumnName) as a default will prevent an odbc error the first time the page is displayed (if it's just linked and not submitted to). Putting strVar2 inside a '%...%' block will search the entire contents of the pertinent column. Leaving out the % will require an exact match.



Ned Frankly
Replied 08 Oct 2002 10:26:09
08 Oct 2002 10:26:09 Saeed Abdi replied:
Thanks guys <img src=../images/dmxzone/forum/icon_smile_big.gif border=0 align=middle>

Reply to this topic