下面的代码主要是实现智能搜索, function autokey是后来加上的, 可就是加上后,程序运行总是出错, “错误类型:Microsoft VBScript 编译器错误 (0x800A03F6)/lgx/案例二十二/find.asp, 第 120 行”,该错误意思是缺少END, 我已经试过了20次,没有改好。 请高手指教!!!! 谢谢!!!!!!!!!!!!!
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>ASP文章集</title> <link rel="stylesheet" type="text/css" href="news.css">
<Script language="JavaScript"> file://打开新窗口函数NewsWindow function NewsWindow(id) { window.open('newswind.asp?id='+id,'infoWin', 'height=400,width=600,scrollbars=yes,resizable=yes'); } </Script> </head>
<body> <% function autokey(strkey) const lngsubkey=2 dim lnglenkey, strnew1, strnew2, i, strsubkey
'检测字符串的合法性,若不合法则转到出错页。出错页你可以根据需要进行设定。 if instr(strkey,"=")<>0 or instr(strkey,"`")<>0 or instr(strkey,"'")<>0 or instr(strkey," ")<>0 or instr(strkey," ")<>0 or instr(strkey,"'")<>0 or instr(strkey,chr(34))<>0 or instr(strkey,"\")<>0 or instr(strkey,",")<>0 or instr(strkey,"<")<>0 or instr(strkey,">")<>0 then response.redirect "error.htm" end if
lnglenkey=len(strkey) select case lnglenkey case 0 '若为空串,转到出错页 response.redirect "error.htm" case 1 '若长度为1,则不设任何值 strnew1="" strnew2="" case else '若长度大于1,则从字符串首字符开始,循环取长度为2的子字符串作为查询条件 for i=1 to lnglenkey-(lngsubkey-1) strsubkey=mid(strkey,i,lngsubkey) strnew1=strnew1 & " or u_name like '%" & strsubkey & "%'" strnew2=strnew2 & " or u_info like '%" & strsubkey & "%'" next end select
'得到完整的sql语句 autokey="select * from t_sample where u_name like '%" & strkey & "%' or u_info like '%" & strkey & "%'" & strnew1 & strnew2
end function
'rs.close 'set rs=nothing 'conn.close 'set conn=nothing set conn=server.createobject("ADODB.CONNECTION") conn.open "DBQ="+server.mappath("news.mdb")+";DRIVER={Microsoft Access Driver (*.mdb)};" set rs=server.createobject("adodb.recordset")
dim s_key,rs,strsql s_key = trim(request("key")) '得到搜索关键字的值 if s_key <>"" then strsql=autokey(s_key) '此处使用自定义函数 autokey(),该函数为实现智能搜索的核心 rs.open strsql,cnn,3,2 '得到搜索后的记录
if rs.eof and rs.bof then response.write "<p>还 没 有 任 何 文 章</p>" else%> <p align=center><strong>ASP全部文章</strong><br><br> <form action="find.asp" method="post"> <p>请输入查询关键字:<input type="text" size=50 name="key" > <input type="submit" value="查询" </p> </form>
<table border=1 borderColorDark=#ffffec borderColorLight=#5e5e00 cellPadding=1 cellSpacing=0 width=100%> <tr bgcolor=cccccc align=center><td>标题</td><td>作者</td><td>日期</td></tr> <% cc=1 '当前页 if not isempty(request("page")) then pagecount=cint(request("page")) else pagecount=1 end if rs.PageSize=10 rs.AbsolutePage=pagecount For iPage = 1 To rs.PageSize If rs.EOF Then Exit For '不同行之间以不同颜色显示 if ipage mod 2=1 then Response.Write "<tr bgcolor=#E7E7E7>" else Response.Write "<tr BGCOLOR=#F4F4F4>" end if %> <td><a href="javascript:NewsWindow(<%=rs("ID")%>)"><u><%=rs("title")%></u></a></td><td><%=rs("write")%> </td><td><%=rs("times")%> <% '如果是今天的新闻,加上new标记 if DateDiff("d",rs("times"),date())<1 then Response.Write "<font color=ff0000>new</font>" Response.Write "</td></tr>" cc=cc+1 rs.movenext Next Response.Write "</table><p>共"&rs.recordcount&"篇文章" if rs.PageCount>1 Then If pagecount<>1 Then Response.Write "<A HREF=default.asp?Page=1>首页</A>" Response.Write "<A HREF=default.asp?Page="&(pagecount-1)&">前页</A>" End If If pagecount<>rs.PageCount Then Response.Write "<A HREF=default.asp?Page="&(pagecount+1)&">后页</A>" Response.Write "<A HREF=default.asp?Page="&rs.PageCount&">尾页</A>" End If End If end if rs.close set rs=nothing conn.close set conn=nothing %><p align=center><a href="newsedit.asp">编辑新闻</a> </body> </html>