回 帖 发 新 帖 刷新版面

主题:求助!!!!

我想实现的功能就是实现多条件的数据检索,有个添加条件按钮,点一下生成一个条件

行,然后输入条件,关键词,即可检索.代码如下

set rs=server.CreateObject("adodb.recordset")
if request.form("检索")<>empty then
  dim a(11),q,r,b(10)
  for q=1 to 11
    a(i)=request.Form("select"&q)
  next
   b(1)=request.Form("guanjianci1")
  for r=2 to 10 step 2
   b(i)=request.Form("guanjianci"&r)
  next
   rs.open "select * from product where "&a(1)&" like '%%"&b(1)&"%%' "&a(3)&" "&a(2)&" like '%%"&b(2)&"%%' "&a(5)&" "&a(4)&" like '%%"&b(4)&"%%' "&a(7)&" "&a(6)&" like '%%"&b(6)&"%%' "&a(9)&" "&a(8)&" like '%%"&b(8)&"%%' "&a(11)&" "&a(10)&" like '%%"&b(10)&"%%',conn,1,1

else
   rs.open "select * from product order by "&px&"",conn,1,1
end if
%>
<%
if rs.eof then
 server.Transfer("error.asp")
%>
.........

其中a(1,2,4,6,8,10)是检索的列(如"商品名称"),a(3,5,7,9,11)是"与"和"或"
b(1,2,4,6,8,10)是关键词.所有列类型都是文本.不知道哪错了???

回复列表 (共5个回复)

沙发

你where条件与条件之间都没有and?
其实你把生成的sql语句response.write 出来很明显就能看出问题了。

板凳

a(3,5,7,9,11)

就是and 或 or

response.write  rs.open "select * from product where "&a(1)&" like '%%"&b(1)&"%%' "&a(3)&" "&a(2)&" like '%%"&b(2)&"%%' "&a(5)&" "&a(4)&" like '%%"&b(4)&"%%' "&a(7)&" "&a(6)&" like '%%"&b(6)&"%%' "&a(9)&" "&a(8)&" like '%%"&b(8)&"%%' "&a(11)&" "&a(10)&" like '%%"&b(10)&"%%',conn,1,1

错误啊

3 楼

like 后面只需要一个%,如: like '%" & aaaa & "%'
两个字段之间需要用and或or来连接。如 sql="select * from table where field like '%"&aaa&"%' and field1 like '%"&bbb&"'"
然后你在执行的之间将sql输出来看看是什么,看看是否是你想要的

4 楼

我用的是access数据库一定要%%的我试过了啊有用的

a(3,5,7,9,11)返回的值就是 "and" 或 "or" 啊,他们返回的都是变量啊

5 楼

你那种写法,检查错误的时候不方便,改成
sql="select ……"
rs.open sql,conn,1,1

如果你遇到问题的时候,你在sql与rs.open 之间加上
response.write sql
response.end
这样可以将sql语句输出,看看它是否正确。

我来回复

您尚未登录,请登录后再回复。点此登录或注册