通过区域来搜索数据库

提供搜索数据库能让用户查询你目标哪个元件的功能。然而允许用户针对数据库的分配,他们会查询给出额外值。在这个例子中,利用我们的美国州的数据库(州,州的名称和首都),用户会搜索任何的三个区域(通过下拉菜单输入)而不是目标的特定的表格元件应用。注意:数据库只有50个意见,因此最好是搜索单字母和双字母的组合。
<html>
<title>CodeAve.com(Search db by Field)</title>
<body bgcolor="#FFFFFF">
<%
u_input = trim(request.form("u_input")) 'u_input is the varible for user input 
‘剪切功能会删除使用输入周围的任何空白
u_field=request.form("u_field") 'the dropdown varible for user inputted search field


if u_input <> "" then ' If the user entered a value query the db
accessdb="state_info" ' name of the access db 
table_name="states" ' name of the table within the access db
cn="driver={microsoft access driver (*.mdb)};"
cn=cn & "dbq=" & server.mappath(accessdb)
set rs = server.createobject("ADODB.Recordset")
sql = "select * from "& table_name &" where " & u_field & " like '%%" & u_input & "%%' " 
response.write sql
rs.Open sql, cn


if rs.eof or rs.bof then 
response.write "No results found..." 
observations=0
else
observations=1
end if 'end check for observations
end if 'end check for user input
%>
<% 'If there are observations then display them 
if observations > 0 then %>
<table>
<tr>
<% 'Write the field names
for each table_element in rs.fields %>
<td><b><%= table_element.name%></B></TD><%
next %>
</tr>
<tr>
<% 'Write the values
rs.movefirst
do while not rs.eof
for each cell in rs.fields %>
<td><%= cell.value %></td><% 
next %>
</tr>
<% rs.movenext
loop %>
</table>
<% end if 'end of check of obs for display %>




<form action ="<%= request.servervariables("script_name") %>" method="post">
<input type="text" name="u_input" value="<%= u_input %>">
<select name="u_field" size="1">
<option <% ' write out all the search fields and select
if u_field = "state" or u_field = "" then
response.write "selected "
end if
%>value="state">State</option>
<option <%
if u_field = "statename" then
response.write "selected "
end if
%>value="statename">State Name</option>
<option <%
if u_field = "capital" then
response.write "selected "
end if
%>value="capital">Capital</option>
</select>
<input type="submit" value="Submit">

</form>

简单生活,希望对大家有用!要觉得本文有价值请分享给您的朋友,生活就是人来人往,分享越多!收获越多!作者:天天ASP家园

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值