Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Searching a Database with multiple fields
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Searching a Database with multiple fields

 Post Reply Post Reply
Author
wwwscripts View Drop Down
Newbie
Newbie
Avatar

Joined: 09 May 2003
Location: United States
Status: Offline
Points: 11
Post Options Post Options   Thanks (0) Thanks(0)   Quote wwwscripts Quote  Post ReplyReply Direct Link To This Post Topic: Searching a Database with multiple fields
    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

Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post 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

Back to Top
Bluefrog View Drop Down
Senior Member
Senior Member


Joined: 23 October 2002
Location: Korea, South
Status: Offline
Points: 1701
Post Options Post Options   Thanks (0) Thanks(0)   Quote Bluefrog Quote  Post ReplyReply Direct Link To This Post 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?

 

Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post Posted: 25 May 2003 at 3:30am
Read up on SQL injections on google aswell...
Back to Top
wwwscripts View Drop Down
Newbie
Newbie
Avatar

Joined: 09 May 2003
Location: United States
Status: Offline
Points: 11
Post Options Post Options   Thanks (0) Thanks(0)   Quote wwwscripts Quote  Post ReplyReply Direct Link To This Post 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

Back to Top
wwwscripts View Drop Down
Newbie
Newbie
Avatar

Joined: 09 May 2003
Location: United States
Status: Offline
Points: 11
Post Options Post Options   Thanks (0) Thanks(0)   Quote wwwscripts Quote  Post ReplyReply Direct Link To This Post Posted: 26 May 2003 at 11:37am
got it working guys... It was a really stupid thing that I had to correct... 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.