Connecting Tech Pros Worldwide Forums | Help | Site Map

search query and protect this text box against sequel injection

Familiar Sight
 
Join Date: Oct 2008
Posts: 128
#1: Feb 3 '09
i have a database as
table name school
field name location
eg in location column data
green school,tagore garden,chink road,jammu
i want to make search on location such that when user enter
green
green school, tagore
search words should come continous
problem
on entering
green school, jammu
record not show
search query is
select*from school where location like'%a%'
where a is variable of text box
also suggest me to protect this text box against sequel injection

Soniad's Avatar
Newbie
 
Join Date: Jan 2009
Location: Mumbai
Posts: 31
#2: Feb 3 '09

re: search query and protect this text box against sequel injection


Quote:

Originally Posted by kkshansid View Post

i have a database as
table name school
field name location
eg in location column data
green school,tagore garden,chink road,jammu
i want to make search on location such that when user enter
green
green school, tagore
search words should come continous
problem
on entering
green school, jammu
record not show
search query is
select*from school where location like'%a%'
where a is variable of text box
also suggest me to protect this text box against sequel injection


In ASP Vbscript , when u r comparing form variables in sql statements, use within quotes as shown below :

sql = "select * from school where location like '%"&a&"%'"

then execute this statement

Regards,
"D"
Familiar Sight
 
Join Date: Oct 2008
Posts: 128
#3: Feb 4 '09

re: search query and protect this text box against sequel injection


eg in location column data
green school,tagore garden,chink road,jammu
i want to make search on location such that when user enter
green
green school, tagore
broblem is that it takes condition that search words should come continous
problem
on entering
green school, jammu
record not show
search query is
sql = "select * from school where location like '%"&a&"%'"
where a is variable of text box
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,691
#4: Feb 9 '09

re: search query and protect this text box against sequel injection


Quote:

Originally Posted by kkshansid View Post

When my users enter multiple search terms often no result is showed. For example when they search by location, the field being searched might contain the following data: "green school,tagore garden,chink road,jammu". If the user enters "green" or "green school, tagore" it returns the correct record, but if the search terms are not contiguous the search fails, for example "green school, jammu" no results are returned. My search query is

Expand|Select|Wrap|Line Numbers
  1. sql = "select * from school where location like '%"&a&"%'"
where a is the text entered in the text box

It looks like your grasp of ASP is OK, but the SQL needs to be more complicated. Try splitting the entered data by the space character, and enter each term separately into the sql like this:
Expand|Select|Wrap|Line Numbers
  1. asplit = split(a, " ") 'asplit is now an array of the terms in a
  2. sql = "SELECT * FROM school WHERE "
  3. for each x in asplit
  4.    sql = sql + "location LIKE '%" & x & "% AND "
  5. next
  6.  
  7. 'sql now ends with "AND " and this needs to be removed
  8. sql = left(sql, len(sql)-4) 'removes the last 4 characters from the string
Try this and let me know if it works.

Jared
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,691
#5: Feb 10 '09

re: search query and protect this text box against sequel injection


Quote:

Originally Posted by kkshansid View Post

also suggest me to protect this text box against sequel injection

In order to protect against SQL injection, you will need to search and replace special characters in your text - the first one that comes to mind is the semi-colon (;) since you need to separate sql statements with the semicolon. There are a whole bunch of characters it would be good to replace, some people have lists, anyway, the basic replace function looks like this:
Expand|Select|Wrap|Line Numbers
  1. searchString = Replace(a, ";", "|")
Jared
Reply