再來(lái)小總結(jié)一下:對(duì)數(shù)據(jù)庫(kù)的操作無(wú)非就是顯示記錄,插入記錄,修改記錄,刪除記錄,查詢(xún)記錄。
并且在有關(guān)顯示記錄的時(shí)候還涉及到顯示的輸出格式、分頁(yè)程序,那么再結(jié)合插入記錄,一個(gè)簡(jiǎn)單的新聞系統(tǒng),文章系統(tǒng),留言系統(tǒng),注冊(cè)登陸系統(tǒng)不成任何問(wèn)題。
下面就涉及到管理這塊,需要對(duì)數(shù)據(jù)庫(kù)記錄進(jìn)行修改。
首先,要修改哪條
修改,不是籠而統(tǒng)之的,而是要針對(duì)某條具體對(duì)相應(yīng)修改??梢孕蜗蟮卣f(shuō),就是對(duì)數(shù)據(jù)庫(kù)表中的具體哪一行進(jìn)行具體的修改。
所以,這時(shí)候的記錄集就有它特定的某個(gè),當(dāng)然這個(gè)主要還是由SQL語(yǔ)句來(lái)決定的。
比如 sql="select * from table where id=1" 就表示提取的id編號(hào)是1的那行的所有記錄,然后只要將該行中需要修改的字段賦以新值然后上傳數(shù)據(jù)庫(kù)就OK了。
同樣的語(yǔ)句 sql="select * from table where id=2" 相信你也能明白。
但作為我們?cè)陧?yè)面中,可不是就這樣固定的,有可是選擇某連接,或者輸入某表單值……跳轉(zhuǎn)到專(zhuān)門(mén)的修改頁(yè),這樣所有的任務(wù)全在修改頁(yè)上了,它所具備的SQL語(yǔ)句應(yīng)該是適應(yīng)性強(qiáng)的
比如 sql="select * from table where id="&request.queyrstring("id")
其次,將要修改的對(duì)應(yīng)賦值
很簡(jiǎn)單,正如插入記錄一樣,將字段和值對(duì)應(yīng)起來(lái)。
rs("cn_name")="cnbruce"
rs("cn_sex")="male"
對(duì)應(yīng)的值當(dāng)然也可以是某個(gè)變量或函數(shù)
最后,上傳更新數(shù)據(jù)庫(kù)
和插入一樣進(jìn)行rs.updata ,其實(shí)觀察下來(lái),插入新記錄和更新記錄只是多了rs.addnew這行的聲明。
1,showit.asp
該文件是前面例中所建立引用的。其主要是顯示的作用,那么現(xiàn)在,針對(duì)具體的某條記錄增加跳轉(zhuǎn)到修改頁(yè)的超級(jí)鏈接。
<%
For i = 1 to rs.PageSize '利用for next 循環(huán)依次讀出當(dāng)前頁(yè)的記錄
if rs.EOF then
Exit For
end if
response.write("<a href=change.asp?id="& rs("cn_id") &">修改</a>")
response.write("文章標(biāo)題是:"& rs("cn_title"))
response.write("<br>文章作者是:"& rs("cn_author"))
response.write("<br>文章加入時(shí)間是:"& rs("cn_time"))
response.write("<br>文章內(nèi)容是:"& rs("cn_content"))
response.write("<hr>")
rs.MoveNext
Next
%>
|
注意response.write("<a href=change.asp?id="& rs("cn_id") &">修改</a>")
后面的參數(shù)id的值則是動(dòng)態(tài)的,那接著就看chang.asp的能耐了。
2,change.asp
<!--#include file="conn.asp" -->
<%
id=request.querystring("id")
%>
<%if request.form("submit")="change" then
whattitle=request.form("title")
whoauthor=request.form("author")
whatcontent=request.form("content")
id=request.form("id")
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from cnarticle where cn_id="&id
rs.Open sql,conn,3,2
rs("cn_title")=whattitle
rs("cn_author")=whoauthor
rs("cn_content")=whatcontent
rs.update
rs.close
Set rs = Nothing
conn.close
set conn=Nothing
response.redirect("showit.asp")
response.end
%>
<%end if%>
<%
if id<>"" then
Set rs = Server.CreateObject ("ADODB.Recordset")
sql="select * from cnarticle where cn_id="&id
rs.Open sql,conn,1,1
whattitle=rs("cn_title")
whoauthor=rs("cn_author")
whatcontent=rs("cn_content")
end if
%>
<form action="change.asp" method="post">
Title:<input type="text" name="title" value=<%=whattitle%>><br>
Author:<input type="text" name="author" value=<%=whoauthor%>><br>
Content:<br>
<textarea name="content" rows="8" cols="30"><%=whatcontent%></textarea><br>
<input type="submit" value="change" name="submit">
<input type="reset" value="Reset">
<input name="id" type="hidden" value="<%=id%>">
</form>
|
當(dāng)然所有的檢察,安全防護(hù)都還沒(méi)做,BUG多多,自己也來(lái)慢慢解決。
另外一類(lèi)的修改更新
<%if request.form("submit")="change" then
whattitle=request.form("title")
whoauthor=request.form("author")
whatcontent=request.form("content")
id=request.form("id")
sql = "update cnarticle set cn_title='"&whattitle&"',cn_author='"&whoauthor&"',cn_content='"&whatcontent&"' where cn_id="&id
conn.Execute(sql)
conn.close
set conn=Nothing
response.redirect("showit.asp")
response.end
%>
|