Print Page | Close Window

Searching a Database with multiple fields

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=2996
Printed Date: 29 March 2026 at 2:58pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Searching a Database with multiple fields
Posted By: wwwscripts
Subject: Searching a Database with multiple fields
Date Posted: 24 May 2003 at 4:33pm

Here is the thing. I am in need of a search script that can search three columns using an And/Or feature automatically.

To simply put, I got an Access Database, with three columns (field1,field2,field3).

I have a forum with three fields (field1,field2,field3).

If some only field1 to search, it will be just

Select * from tableName where field1 = '%" & Request.Form("field1") & "%'"

If some one decides to search using field1 and field 2

Select * from tableName where field1 = '%" & Request.Form("field1") & "%' and field1 = '%" & Request.Form("field2") & "%'"

Please ASSIST and THANKS in advance...

Shrini



-------------
Welcome to the New world



Replies:
Posted By: michael
Date Posted: 24 May 2003 at 9:52pm

That's an very easy example on how you could do it, sure there are better ways but should lead you in the right direction. Make sure to filter the input of the fields agains injection plus some error handling in case someone leaves fields blank when they are not supposed to....

field1 = Request.QueryString("field1")
field2 = Request.QueryString("field2")
field3 = Request.QueryString("field3")
SQL = "Select * from tableName where field1 = '%" & field1 & "%'"
If len(field2) > 0 THEN
SQL = SQL & " OR field2 = '%" & field2 & "%'"
END IF
If len(field3) > 0 THEN
SQL = SQL & " OR field3 = '%" & field3 & "%'"
END IF



-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: Bluefrog
Date Posted: 24 May 2003 at 10:00pm

How about something like:

strSQL = "Select * from tableName "

If Request.Form("field1") <> "" and Request.Form("field2") <> "" and Request.Form("field3")<>"" Then
    strSQL = strSQL & " where "

If Request.Form("field1") <> "" then
    strSQL = strSQL & "field1 = '%" & Request.Form("field1") & "%'"
    if Request.Form("field2")<> "" or Request.Form("field3")<> "" then strSQl = strSQL & " and "
End if

 

if Request.Form("field2")<> "" then
    strSQL = strSQL & "field2 = '%" & Request.Form("field1") & "%'"
    if Request.Form("field3")<> "" then strSQL = strSQL & " and "
End ifif Request.Form("field3")<> "" then
    strSQL = strSQL & "field3 = '%" & Request.Form("field1") & "%'"
End if

End if

Is that sort of what you are lookig for?

 



Posted By: Mart
Date Posted: 25 May 2003 at 3:30am
Read up on SQL injections on google aswell...


Posted By: wwwscripts
Date Posted: 26 May 2003 at 10:38am

Well I figured out a way to do that. It took little technical thinking. But stuck with another SQL Statement problem which is working little strange when I try to right it.

SELECT
user.id,
user.username,
user.password,
user_info.fullname
FROM user
INNER JOIN
user_info ON
user.id = user_info.id


I executed the statement with my dbConn and wrote in the page using the following...

do while not rs.eof
Response.Write id & " | " & username & " | " & password & " | " & fullname
rs.movenext
loop

There are 12 records, that match in the two tables, using the id column.

But when it writes, it takes the first record and writes it 12 times. Any Ideas...

Thanks in Advance,

Shrini



-------------
Welcome to the New world


Posted By: wwwscripts
Date Posted: 26 May 2003 at 11:37am
got it working guys... It was a really stupid thing that I had to correct... 

-------------
Welcome to the New world



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net