数据库实现显示、添加、修改、删除的最简单的方法及实例

80酷酷网    80kuku.com

  数据|数据库|显示

  关于本文:

  本着互联网共享主义精神,特写此文献给建站新手,授人以鱼,不如授人以渔,有人看完后,可能会觉得代码不太完善,因为发表本文的目地,是向大家介绍实现的原理,而不是给大家一套完整的程序,希望大家看完此文,能够从中得到些帮助和启发。

  本文用最精简的实例介绍了一种实现显示、添加、修改、删除的比较简便的方法,虽然是以ASP例子介绍,但同样适用于PHP、JSP、ASP.NET等语言。文中用到了javascript脚本,大家也可从中学到部分的javascript知识。

  因为近几天工作太忙,就不祥细的写原理文章了,我在代码里面写的注释非常清楚,相信懂点网页编程的朋友应该都能够看懂。

  声明:希望各位朋友转载时,不要把原有作者版权去掉,谢谢合作。

  由于本人写作水平有限,写的不好的地方,还请大家多多包涵,如要批评或意见,请加我QQ。

  以下正文开始:

  功能介绍:

  平时很多人至少需要做三个表单才能实现数据的显示、添加、修改、删除,本文讲的是只用一个添加表单,就可实现数据的显示、添加、修改、删除功能。用此方法写程序时,至少可以省两倍的精力。

  实现例子:

  例子说明:

数据库名:db.mdb
表名:pub_doc
字段:id和memo1-----memo30 (多少个字段大家可以自己设置)
公用部分:
公用部分,可以存成单个文件,用include调用。

<%
'数据库连接
db="db.mdb"
set conn=server.createobject("adodb.connection")
conn.open "provider=microsoft.jet.oledb.4.0;data source="& server.mappath(db)

'发布时间:2005年12月30日 晚
'文章作者:翟振凯 (小琦)
'个人站:   
'技术站:
'商业站:
'论坛站:
'QQ:53353866 22336848

'添加数据函数
function add_form(sl)'添加数据函数

'response.write request("act")
'response.end

if request("act")="add_save" and sl<>"" then

'------------------生成SQL语句----------------小琦

'--------循环memo
                        For i = 1 To sl
                                zd_name=zd_name&"memo"&i&","
                        if request("memo"&i&"")<>"" then                               
                                zd_value=zd_value&"'"&request("memo"&i&"")&"'"&","
                        else
                                zd_value=zd_value&"'"&null&"'"&","
                        end if                       
                        next
'//--------循环memo

'去掉最后的“,”
zd_name=left(zd_name,len(zd_name)-1)
zd_value=left(zd_value,len(zd_value)-1)

sql="Insert into pub_doc("&zd_name&")values("&zd_value&")"

'//------------------生成SQL语句----------------小琦
conn.execute(sql)
response.write "添加数据成功!"
Response.End
end if

end function

'修改数据函数
function edit_form(sl)'修改数据函数

if request("id")<>"" and request("act")="edit_save" and sl<>"" then

'------------------生成SQL语句----------------小琦
               
'--------循环memo
                zd_name="" '字段名子
                        For i = 1 To sl
                                zd_name=zd_name&"memo"&i&"="
                        if request("memo"&i&"")<>"" then                               
                                zd_name=zd_name&"'"&request("memo"&i&"")&"'"&","
                        else
                                zd_name=zd_name&"'"&null&"'"&","
                        end if                       
                        next
'//--------循环memo

'去掉最后的“,”
zd_name=left(zd_name,len(zd_name)-1)

'//------------------生成SQL语句----------------小琦

sql="Update pub_doc set "&zd_name&" where id="&request("id")&""
conn.execute(sql)

response.write "修改成功!"

Response.End()
end if
end function

function read_edit_form(sl)'修改前读取数据函数
if request("id")<>"" and request("act")="edit" and sl<>"" then'读取要修改的数据

'------------------生成SQL语句----------------小琦
                        zd_name="" '字段名子
'--------循环memo
                        For i = 1 To sl
                                zd_name=zd_name&"memo"&i&","       
                        next
'//--------循环memo

'去掉最后的“,”
zd_name=left(zd_name,len(zd_name)-1)

sql="select top 1 "&zd_name&" from pub_doc where id="&request("id")&""
'//------------------生成SQL语句----------------小琦
set rs = conn.execute(sql)'查询

response.write "<script language=javascript>"'写javascript的脚本
response.write "function read_data(){"'

'--------循环赋值
                        for each i in split(zd_name,",")
                                response.write "frm."&i&".value="""&Replace(Replace(Replace(rs(i)&"||",chr(13),"\n"),chr(10),"\n"),"||","")&""";"'
                        next
'//--------循环赋值

response.write "}</script>"

end if
end function

function read_form(sl)'读取数据函数
if request("id")<>"" and request("act")="" and sl<>"" then'读取要查看的数据

'------------------生成SQL语句----------------小琦
'--------循环memo
                        For i = 1 To sl
                                zd_name=zd_name&"memo"&i&","       
                        next
'//--------循环memo

'去掉最后的“,”
zd_name=left(zd_name,len(zd_name)-1)

sql="select top 1 "&zd_name&" from pub_doc where id="&request("id")&""
'//------------------生成SQL语句----------------小琦
set rs = conn.execute(sql)'查询

response.write "<sc"+"ript language=javascript>"'写javascript的脚本
response.write "function read_data(){"'
'--------循环赋值
                        for each i in split(zd_name,",")
                        if rs(i)="" then kongge=" "'如果内容为空,则用空格代替
                                response.write "frm."&i&".parentElement.innerText="""&rs(i)&kongge&""";"'
                        next
'//--------循环赋值
response.write "}</scr"+"ipt>"
end if
end function

function del()'删除数据

if request("id")<>"" and request("act")="del" then

conn.execute("Delete from pub_doc where id="&request("id")&" ")
response.write "删除成功!"
response.end

end if

end function

function xiaoqi_end()
if request("id")<>"" and request("act")="" then response.write "<sc"+"ript language=javascript>frm.save.removeNode(true);read_data()</sc"+"ript>"
if request("id")<>"" and request("act")="edit" then response.write "<sc"+"ript language=javascript>read_data()</sc"+"ript>"
session("act")=""
end function

function get_act(sl)
add_form       sl
edit_form      sl
read_edit_form sl
read_form      sl
del
end function

'设置动作
if request("act")="add" then session("act")="add_save"
if request("act")="edit" then session("act")="edit_save"
%>

  调用实例

<%get_act("26")‘使用了26个字段%>
<% if request("act")<>"" or request("id")<>"" then’如果不是显示列表页面%>

<p> </p>
<form method="POST" action="index.asp" name="frm">
        <div align="center">
                <table border="1" width="600" id="table2" bordercolorlight="#000000" cellspacing="0" cellpadding="0" bordercolordark="#FFFFFF" center">
                        <tr>
                                <td width="149">数据表</td>
                                <td width="149"><input type="text" name="memo1"></td>
                                <td width="150"><input type="text" name="memo2"></td>
                                <td width="150"><input type="text" name="memo3"></td>
                        </tr>
                        <tr>
                                <td width="149"><input type="text" name="memo4"></td>
                                <td width="149"><input type="text" name="memo7"></td>
                                <td width="150"><input type="text" name="memo6"></td>
                                <td width="150"><input type="text" name="memo5"></td>
                        </tr>
                        <tr>
                                <td width="149"><input type="text" name="memo9"></td>
                                <td width="149"><input type="text" name="memo10"></td>
                                <td width="150"><input type="text" name="memo11"></td>
                                <td width="150"><input type="text" name="memo12"></td>
                        </tr>
                        <tr>
                                <td width="149"><input type="text" name="memo16"></td>
                                <td width="149"><input type="text" name="memo15"></td>
                                <td width="150"><input type="text" name="memo14"></td>
                                <td width="150"><input type="text" name="memo13"></td>
                        </tr>
                        <tr>
                                <td width="149"><input type="text" name="memo17"></td>
                                <td width="149"><input type="text" name="memo18"></td>
                                <td width="150"><input type="text" name="memo19"></td>
                                <td width="150"><input type="text" name="memo20"></td>
                        </tr>
                        <tr>
                                <td width="149"><input type="text" name="memo24"></td>
                                <td width="149"><input type="text" name="memo23"></td>
                                <td width="150"><input type="text" name="memo22"></td>
                                <td width="150"><input type="text" name="memo21"></td>
                        </tr>
                        <tr>
                                <td width="149"><input type="text" name="memo25"></td>
                                <td width="149"><input type="text" name="memo26"></td>
                                <td width="150"><input type="text" name="memo8"></td>
                                <td width="150"><input type="submit" value="提交" name="save"></td>
                        </tr>
                </table>
        </div>
<input type="hidden" name="id" value="<%=request("id")%>">
<input type="hidden" name="act" value="<%=session("act")%>">
</form>
<p> </p>

<%
xiaoqi_end()
else%>

<p align="center"><a href="?act=add">添加</a></p>
<div align="center">
        <table border="1" width="600" id="table1" bordercolorlight="#000000" cellspacing="0" cellpadding="0" bordercolordark="#FFFFFF" center">
                <tr>
                        <td width="142">字段1</td>
                        <td width="142">字段2</td>
                        <td width="143">字段3</td>
                        <td width="63">查看</td>
                        <td width="63">修改</td>
                        <td width="63">删除</td>
                </tr>
<%
set rs=server.createobject("adodb.recordset") 
sql = "select * from pub_doc order by id desc "
rs.open sql,conn,1,1
do while not rs.eof
%>               
                <tr>
                        <td width="142"> <%=rs("memo1")%></td>
                        <td width="142"> <%=rs("memo2")%></td>
                        <td width="143"> <%=rs("memo3")%></td>
                        <td width="63"><a href="?id=<%=rs("id")%>">查看</a></td>
                        <td width="63"><a href="?id=<%=rs("id")%>&act=edit">修改</a></td>
                        <td width="63"><a href="?id=<%=rs("id")%>&act=del">删除</a></td>
                </tr>
<%
rs.MoveNext                                             
loop %>
        </table>
</div>
<%end if%>

  再给大家一个我原创的用javascript将文本框转换为文本的例子,相信很多人都会用到。大家存成html文件打开即可看到效果。

  原理:从文本框中取值,把值赋于文本框的父对象。

<script language=javascript>
function read_data(){
frm.memo1.parentElement.innerText="1";
frm.memo2.parentElement.innerText="2";
frm.memo3.parentElement.innerText="3";
}</script>

<p> </p>
<form method="POST" action="index.asp" name="frm">
<div align="center">
<table border="1" width="600" bordercolorlight="#000000" cellspacing="0" cellpadding="0" bordercolordark="#FFFFFF" center">
<tr>
<td width="149">数据表</td>
<td width="149"><input type="text" name="memo1"></td>
<td width="150"><input type="text" name="memo2"></td>
<td width="150"><input type="text" name="memo3"></td>
</tr>
</table>
<p><input type="submit" value="提交" name="save"></div>
</form>
<script language=javascript>frm.save.removeNode(true);read_data()</script>

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: