|
Search Database (SQL Where Form #3)
Ideally, the perfect "pick a city" example would
show people a list of items so they can't choose wrong:
filename=/learn/test/SQLwhereform3.asp
<HEAD><TITLE>SQLwhereform3.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<Form action = "SQLwhereForm3respond.asp" method=GET>
Choose A City:<p>
City:
<%
call query2list("select distinct city from publishers", _
"cy","DSN=student;uid=student;pwd=magic")
%>
<P>
<Input type="submit" value="Get Data"> <Input type="reset" value="Clear City"></form>
</BODY></HTML>
<!--#include virtual="/learn/test/lib_dblist.asp"-->
filename=/learn/test/SQLwhereform3respond.asp
<html><head><TITLE>sqlwhereform3respond.asp</TITLE></head>
<body bgcolor="#FFFFFF">
<%
myDSN="DSN=student;uid=student;pwd=magic"
mycity=request.querystring("cy")
myexactsearch=request.querystring("exactsearch")
SQLtemp="select * from publishers where city"
If myexactsearch="on" then
SQLtemp=SQLtemp & " ='"
Else
SQLtemp=SQLtemp & " LIKE '"
End If
SQLtemp=SQLtemp & mycity & "'"
'response.write SQLtemp
call query2table(SQLtemp,myDSN)
%>
<!--#include virtual="/learn/test/lib_dbtable.asp"-->
</body></html>
The file lib_dbtable.asp looks like this:
filename=/learn/test/lib_dbtable.asp
<%
sub query2table(inputquery, inputDSN)
dim conntemp, rstemp
set conntemp=server.createobject("adodb.connection")
conntemp.open inputDSN
set rstemp=conntemp.execute(inputquery)
howmanyfields=rstemp.fields.count -1%>
<table border=1><tr>
<% 'Put Headings On The Table of Field Names
for i=0 to howmanyfields %>
<td><b><%=rstemp(i).name%></B></TD>
<% next %>
</tr>
<% ' Now lets grab all the records
do while not rstemp.eof %>
<tr>
<% for i = 0 to howmanyfields
thisvalue=rstemp(i)
If isnull(thisvalue) then
thisvalue=" "
end if%>
<td valign=top><%=thisvalue%></td>
<% next %>
</tr>
<%rstemp.movenext
loop%>
</table>
<%
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
end sub%>
The file lib_dblist.asp looks like this:
filename=/learn/test/lib_dblist.asp
<%sub query2list(myquery,myname,myDSN)
dim conntemp, rstemp
set conntemp=server.createobject("adodb.connection")
conntemp.open myDSN
set rstemp=conntemp.execute(myquery)
%>
<Select name="<%=myname%>">
<%
do while not rstemp.eof
thisfield=trim(RStemp(0))
if isnull(thisfield) or thisfield="" then
' ignore
else
response.write "<option>" & thisfield & "</option>"
end if
rstemp.movenext
loop
%>
</select>
<%rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
end sub%>
|