教程|十天學會
上一節:寫入記錄
學習目的:學會資料庫的基本操作--查詢記錄
在第四天中我們用到下面這樣一個程式:
<%
set conn=server.createobject("adodb.connection")
conn.open "driver={microsoft access driver (*.mdb)};dbq="&server.mappath("data/guestbook.mdb")
exec="select * from guest"
set rs=server.createobject("adodb.recordset")
rs.open exec,conn,1,1
%>
我們查詢的是所有的記錄,但是我們要修改、刪除記錄的時候不可能是所有記錄,所有我們要學習檢索合適的記錄。先看一條語句:
a="aaa"
b=1111110
exec="select * from guestbook where name='"+a+"'and tel="+b
where後面加上的是條件,與是and,或是or,我想=,<=,>=,<,>的含義大家都知道吧。這句話的意思就是搜尋name是“aaa”的,並且電話是“1111110”的記錄。還有一點就是如果要搜尋一個欄位裡面是不是包含一個字串就可以這麼寫:where instr(name,a)也就是搜尋name裡面有a(aaa)這個字串的人。
我這裡的a,b,是常量,大家可以讓a,b是表單提交過來的變數,這樣就可以做一個搜尋了。
下面大家看看這個代碼,理解一下:(把下面代碼存為6.htm檔案)
<form name="form1" method="post" action="exam6.asp">
搜尋:<br>
name =
<input type="text" name="name">
and tel=
<input type="text" name="tel">
<br>
<input type="submit" name="Submit" value="提交">
<input type="reset" name="Submit2" value="重設">
</form>
exam6.asp(把下面代碼存為exam6.asp)
<%
name=request.form("name")
tel=request.form("tel")
set conn=server.createobject("adodb.connection")
conn.open "driver={microsoft access driver (*.mdb)};dbq="&server.mappath("data/guestbook.mdb")
exec="select * from guest where name='"+name+"' and tel="+tel
set rs=server.createobject("adodb.recordset")
rs.open exec,conn,1,1
%>
<html>
<head>
<title>無標題文檔</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<%
do while not rs.eof
%><tr>
<td><%=rs("name")%></td>
<td><%=rs("tel")%></td>
<td><%=rs("message")%></td>
<td><%=rs("time")%></td>
</tr>
<%
rs.movenext
loop
%>
</table>
</body>
</html>
首先在瀏覽器中輸入http://localhost/6.htm如下圖所示:
輸入資料庫中已有的姓名和電話aaa,1111110,單擊提交,結果如下圖所示:
下一節:刪除記錄