如何把表单中收集的数据提交到数据库?详细代码该怎么写??
下面是页面代码
1,reg.asp
<html>
<head><title>网页标题拦</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style type="text/css">
<!--
body,td,th {
color: #6633FF;
}
.style1 {font-size: 15px}
.style2 {font-size: 24px}
-->
</style></head>
<body bgcolor="#000000" alink="#bfffaa" link="#777777">
<center>
<p class="style2">用 户 注 册</p>
<hr>
<form name=add method=post action="d_ins.asp">
<table width="48%" height="26%" border="1" bgcolor="#999999">
<tr>
<td width="24%"><dfn>用 户名:</dfn></td>
<td width="76%"><dfn>
<input name="name" type="text" class="style1" size="10">
</dfn></td>
</tr>
<tr>
<td><dfn>密 码:</dfn></td>
<td><dfn>
<input name="pasw1" type="password" class="style1" size="16">
</dfn></td>
</tr>
<tr>
<td><dfn>重复密码:</dfn></td>
<td><dfn>
<input name="pasw2" type="password" class="style1" size="16">
</dfn></td>
</tr>
<tr>
<td><dfn>邮 箱:</dfn></td>
<td><dfn>
<input name="email" type="text" class="style1">
</dfn></td>
</tr>
<tr>
<td><dfn>电 话:</dfn></td>
<td><dfn>
<input name="phone" type="text" class="style1">
</dfn></td>
</tr>
<tr>
<td><dfn>地 址:</dfn></td>
<td><dfn>
<input name="address" type="text" class="style1">
</dfn></td>
</tr>
<tr>
<td><dfn>邮 编:</dfn></td>
<td><dfn>
<input name="zipcode" type="text" class="style1">
</dfn></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<dfn>
<input name="Submit" type="submit" class="style1" value="提交">
<input name="Submit2" type="reset" class="style1" value="清除">
</dfn></div></td>
</tr>
</table>
</form>
<hr>
<br>
<br>
</center>
</body>
</html>
其中数据库的链接文件是:
connection_db.asp
<%
set conn=server.CreateObject("adodb.connection")
conn.provider="microsoft.jet.oledb.4.0"
conn.open server.MapPath("db1.mdb")
set rs=server.CreateObject("adodb.recordset")
%>
数据库名字是db1.mdb
表名为tb
字段为:
t_name,t_pasw1,t_pasw2,t_email,t_phone,t_address,t_postcode
请高手帮我,我自己搞的不明白
<!--#include file="connection_db.asp"-->
<%
set rs=server.CreateObject("adodb.recordset")
sql="select * from tb"
rs.open sql,conn,3,3
rs.addnew
t_name=request.Form("name")
t_pasw1=request.Form("pasw1")
t_pasw2=request.Form("pasw2")
t_email=request.Form("email")
t_phone=request.Form("phone")
t_address=request.Form("address")
t_postcode=request.Form("postcode")
rs.t_name=t_name
rs.t_pasw1=t_pasw1
rs.t_pasw2=t_pasw2
rs.t_email=t_email
rs.t_phone=t_phone
rs.t_address=t_address
rs.t_postcode=t_postcode
rs.update
rs.close
response.write "保存成功"
%>
我写的d_ins.asp怎么还不对?就用那方法写的
<%
set rs=server.CreateObject("adodb.recordset")
sql="select * from tb"
rs.open sql,conn,1,3
rs.addnew
'上面是打开数据表tb,并新建一个插入记录
'我只以前几项数据来说明,其它数据一样用法
,下面用Request.form方法接收上面所说的表单提交过来的数据 name 为 "name" (两个name不要搞混了。)把接收到的数据赋值给t_name
t_name=Request.form("name")
t_pasw1=Request.form("pasw1")
t_pasw2=Request.form("pasw2")
t_email=Request.form("t_email")
……
'依此类推,格式完全一样。这里要对两个密码进行比较,如果不一样得返回提交表单,这个就不细说了。如果两次输入密码一样,则任意保存一个为密码即可。
然后将t_name赋值给新数据集里的t_name项。
rs.t_name=t_name
rs.t_pasw1=t_pasw1
'其实数据表里只需要一条密码项,在保存的时候对两次密码进行判断后保存一个即可。
rs.t_email=t_email
……
'然后对所有的数据赋集写入数据表,用update
rs.update
rs.close
response.write "保存成功!"
%>
这是最基本最简单的,完善的情况还应该涉及到对数据类型的判断,长度的判断等。
<!--#include file="tools.asp" -->
<!--# include file="checkpass.asp" -->
<%
table="tb"
if request.form("addedit_subed")="true" then
_name=Request.Form("_name")
t_pasw1=Request.Form("t_pasw1")
t_phone=Request.Form("t_phone")
t_address=Request.Form("t_address")
'服务器端验证
'outcheck(CheckValue(_name,,,1,"姓名"))
'outcheck(CheckValue(t_pasw1,,,1,"密码"))
'outcheck(CheckValue(t_phone,,,1,"电话"))
'outcheck(CheckValue(t_address,,,1,"地址"))
field1=array("_name","t_pasw1","t_phone","t_address",)
value1=array(_name,t_pasw1,t_phone,t_address,)
'防止二义性信息逻辑****************
'set rs_test=Server.CreateObject("ADODB.RECORDSET")
'rs_test.Open "select count(*) as counter from " & table & " where username='" & username & "'",conn
'if rs_test("counter")>0 then
' call Msgout("用户名已存在, 不能添加!","javascript:history.back()",3)
' Response.End
'end if
'************************************
for i=0 to ubound(field1)
value1(i)=replace(value1(i),"'","''")
next
sql = "INSERT INTO " & table & " (" & Join(field1, ",") & ") VALUES ('" & Join(value1, "','") & "')"
word="你已成功加入!"
conn.Execute sql
end if
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="style.css">
<title></title>
<% if request.form("addedit_subed")<>"true" then '********************* %>
<script language="JavaScript" src="check.js"></script>
<script LANGUAGE="javascript">
<!--
function add_onsubmit(add) {
/* 如需验证二次密码时用
if(add.password.value!=add.repassword.value){
alert('两次密码不相同!');
return false;
}
*/
//客户端脚本验证
//if (!checkvalue(add._name,0,0,1,"姓名")) return false;
//if (!checkvalue(add.t_pasw1,0,0,1,"密码")) return false;
//if (!checkvalue(add.t_phone,0,0,1,"电话")) return false;
//if (!checkvalue(add.t_address,0,0,1,"地址")) return false;
}
//-->
</SCRIPT>
<% end if '************************************************* %>
</head>
<body bgcolor="#FFFFFF">
<!--#include file="top.inc"-->
<% if request.form("addedit_subed")="true" then %>
<center><% =word %></center>
<% else %>
<form method=POST name=add LANGUAGE=javascript onsubmit="return add_onsubmit(this)">
<div align=center>
<table border=0 cellpadding=0 width=435 cellspacing=1>
<input type=hidden name="addedit_subed" value="true">
<tr><td width=208 align=right bgcolor=#D2D8BE><span class=fstyle>姓名:</span></td>
<td width=359><input type=text name="_name" size=20 class=fstyle value="">
</td></tr><tr><td width=208 align=right bgcolor=#D2D8BE><span class=fstyle>密码:</span></td>
<td width=359><input type=text name="t_pasw1" size=20 class=fstyle value="">
</td></tr><tr><td width=208 align=right bgcolor=#D2D8BE><span class=fstyle>信箱:</span></td>
<td width=359><input type=password name="t_email" size=20 class=fstyle value="<% =Server.HTMLEncode(password) %>">
</td></tr><tr><td width=208 align=right bgcolor=#D2D8BE><span class=fstyle>电话:</span></td>
<td width=359><input type=text name="t_phone" size=20 class=fstyle value="">
</td></tr><tr><td width=208 align=right bgcolor=#D2D8BE><span class=fstyle>地址:</span></td>
<td width=359><input type=text name="t_address" size=20 class=fstyle value="">
</td></tr><tr><td width=208 align=right bgcolor=#D2D8BE><span class=fstyle>邮编:</span></td>
<td width=359><input type=password name="t_postcode " size=20 class=fstyle value="<% =Server.HTMLEncode(password) %>">
</td></tr>
</table>
</div>
<div align=center>
<input type=submit value="提交" name=B1 class=fstyle>
<input type=reset value="重填" name=B2 class=fstyle>
</div>
</form>
<% end if %>
<!--#include file="bottom.inc"-->
</body>
</html>