(4) <% rs.movenext loop end if %> 這句是循環(huán)結(jié)束的語句 rs.movenext 這句是游標(biāo)向下指的意思 (5) <% rs.close set rs=nothing conn.close set conn=nothing %> 這句是關(guān)閉數(shù)據(jù)庫的語句 用完數(shù)據(jù)庫后要記得關(guān)閉數(shù)據(jù)庫。。以免占用資源 養(yǎng)成隨手關(guān)門的好習(xí)慣 就學(xué)到這里。。好好消化上面的代碼。。心急吃不了熱豆腐。。 一步一個(gè)腳印最塌實(shí) (第3個(gè)小時(shí)) 現(xiàn)在來學(xué)添加數(shù)據(jù)到數(shù)據(jù)庫 需要兩個(gè)文件來實(shí)現(xiàn) (1)先建立一個(gè)提交表單add.asp 代碼如下: CODE: <html> <head> <meta http-equiv="Content-Type" c /> <title>無標(biāo)題文檔</title> </head> <body> <form name="form1" method="post" action="add_new.asp"> 名字 <input type="text" name="name"><br> 備注 <input type="text" name="content"><br> 題目<input type="text" name="title" ><br> <input type="submit" name="Submit" value="提交"> <input type="reset" name="Submit2" value="重置"> </form> </body> </html> [Copy to clipboard] 上面代碼要注意的是action="add_new.asp"提交到add_new.asp文件 還有name="name" name="content" name="title" 名字要對(duì)應(yīng) (2)處理文件add_new.asp 代碼如下: CODE: <!--#include file="conn.asp"--> <% name=request.form("name") content=request.form("content") title=request.form("title") exec="insert into aa(name,content,title) values('"+name+"','"+content+"','"+title+"')" conn.execute exec conn.close set conn=nothing response.redirect "index.asp" %> 代碼解釋: (1) name=request.form("name") content=request.form("content") title=request.form("title") 這三句語句是接收add.asp發(fā)來的信息。。request.form("name")就是對(duì)應(yīng)add.asp中的name="name" (2) exec="insert into aa(name,content,title) values('"+name+"','"+content+"','"+title+"')" conn.execute exec 上面的語句是數(shù)據(jù)庫插如信息的語句 insert into aa(name,content,title) 中文意思是插入信息對(duì)應(yīng)aa表中的三個(gè)字段 三個(gè)字段的數(shù)值是values('"+name+"','"+content+"','"+title+"') 這里面對(duì)應(yīng)的值是 這三句語句 name=request.form("name") content=request.form("content") title=request.form("title") 傳遞的 (3) conn.close set conn=nothing response.redirect "index.asp" 關(guān)閉數(shù)據(jù)庫。。寫如成功后自動(dòng)轉(zhuǎn)頁面到index.asp 到這里相信你已經(jīng)學(xué)會(huì)如何添加新記錄到數(shù)據(jù)庫了。。 那就跟著我接著學(xué)吧。。。 (第4個(gè)小時(shí)) 接下來我們來學(xué)刪除數(shù)據(jù)庫信息。。。 刪除信息比較簡單。。只需要一個(gè)文件del.asp 代碼如下: CODE: <!--#include file="conn.asp"--> <% exec="delete * from aa where id="&request.querystring("id") conn.execute exec conn.close set conn=nothing response.redirect "index.asp" %> 代碼解釋: exec="delete * from aa where id="&request.querystring("id") conn.execute exec 先翻譯成中文意思 刪除表aa中id字段的值。。這個(gè)值等于="&request.querystring("id") 還記得我們建立index.asp文件的時(shí)候嗎?里面有段語句是這樣的 <td width="32" ><a href="del.asp?id=<%=rs("id")%>">刪除</a></td> 大家現(xiàn)在看明白了吧!你們這么聰明。。一定明白了。 我再補(bǔ)充說兩句 當(dāng)我們點(diǎn)擊刪除的時(shí)候。。。這個(gè)時(shí)候就會(huì)連接到del.asp做處理。。 而同時(shí)del.asp接收到我們的請(qǐng)求是通過"&request.querystring("id")這句語句 然后conn.execute exec 這條語句就執(zhí)行刪除。。 整個(gè)流程就是這樣。。。 希望學(xué)習(xí)的人好好弄清楚。。那樣我們才可以繼續(xù)學(xué)習(xí)修改 (第5個(gè)小時(shí)) 現(xiàn)在學(xué)習(xí)數(shù)據(jù)的修改。 這是難點(diǎn)。。大家要認(rèn)真的跟著我學(xué)。。 本人也是看了很久才學(xué)會(huì)的。。解釋的不好多多包容。。 修改需要兩個(gè)文件 (一)建立modify.asp 和del.asp很像似 代碼如下: CODE: <!--#include file="conn.asp"--> <% exec="select * from aa where id="&request.querystring("id") set rs=server.createobject("adodb.recordset") rs.open exec,conn,1,1 %> <form name="form1" method="post" action="modifysave.asp"> <table width="748" border="0" cellspacing="0" cellpadding="0"> <tr> <td>name</td> <td>content</td> <td>title</td> </tr> <tr> <td> <input type="text" name="name" value="<%=rs("name")%>"> </td> <td> <input type="text" name="content" value="<%=rs("content")%>"> </td> <td><input type="text" name="title" value="<%=rs("title")%>"> <input type="submit" name="Submit" value="提交"> <input type="hidden" name="id" value="<%=request.querystring("id")%>"> </td> </tr> </table> </form> <% rs.close set rs=nothing conn.close set conn=nothing %> 代碼解釋: (1) <% exec="select * from aa where id="&request.querystring("id") set rs=server.createobject("adodb.recordset") rs.open exec,conn,1,1 %> 這句exec="select * from aa where id="&request.querystring("id")我在del.asp里解釋過了 在這里也是一樣的道理 set rs=server.createobject("adodb.recordset") rs.open exec,conn,1,1 上面是用recordset對(duì)象打開數(shù)據(jù)表。。。 (2) <input type="text" name="name" value="<%=rs("name")%>"> 這里要注意名字要對(duì)應(yīng)。。否則或出錯(cuò)。。 <input type="hidden" name="id" value="<%=request.querystring("id")%>"> 這里是隱藏表單。。。提交隱藏的表單元素會(huì)隨著表單一起提交,用于傳遞變量 |
聯(lián)系客服