利用mysql和asp写一个注册程序

上一篇博客写到了asp与mysql创建一个简单的链接,此次拿一个用户注册程序练手。html

先附上跑通的服务器端代码:前端

<html>
<head>
<meta meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>成功!</title>
</head>
<body>
<%
strconnection="dns=ODBC57;driver={mysql odbc 5.3 ansi driver};database=first(表名);server=localhost;uid=root;password=*********"
set conn = server.createobject("adodb.connection")
conn.open strconnection
%>
<%
ac=request.QueryString("ac")
msg=" 注册错误信息 "
if request.form("username")="" then
	msg=msg&"<br>"&" 用户名不能为空 "
end if
if strcomp(cstr(request.form("password")),cstr(request.form("password2")))<>0 then
	msg=msg&"<br>"&" 两次密码输入不一样 "
end if
if len(request.form("password"))<6 then
	msg=msg&"<br>"&" 密码太简单 "
end if
if strcomp(msg," 注册错误信息 ")>0 then
	response.Redirect("reg.asp?msg="&msg)
end if
if ac="adduser" then
	set rsc=server.createobject("adodb.recordset")
	sql="select * from user where username ='"&request.form("username")&"'"
	rsc.open sql,conn,1,1
	if rsc.eof and rsc.bof then
	else
	ck=rsc("username")
	end if
	set rsc=nothing
	if ck<>"" then
		msg=msg&"<br>"&" 用户名被人注册 "
		response.Redirect("reg.asp?msg="&msg)
	end if
	dsql="select * from user where id is null"
	set rs=server.createobject("adodb.recordset")
	rs.open dsql,conn,1,3
	rs.addnew
	rs("username")=request.form("username")
	rs("password")=request.form("password")
	rs("mail")=request.form("mail")
	rs("sex")=request.form("qq")
	rs("address")=request.form("add")
	rs("ntime")=now
	rs.update
	set rs=nothing
%>
<center>
<a href="index.asp" target="_self">注册成功,点击登录</a>
</center>
<%
end if
%>
</body>
</html>

###使用connection对象链接到数据库mysql

strconnection="dns=ODBC57;driver={mysql odbc 5.3 ansi driver};database=first(表名);server=localhost;uid=root;password=*********"

该行代码指定链接字符串,并将它赋给strconnection变量。dns可在系统dns变量中设置。driver在odbc数据源中查看,database为数据库名,server暂时为本地计算机localhost。uid为数据库用户名,password为数据库密码。 因为VBScript中无需先声明变量再给变量赋值,因此在此处直接使用strconnection变量。sql

set conn = server.createobject("adodb.connection")

建立一个connection对象的实例,并将它赋值给conn变量。数据库

conn.open strconnection

打开链接字符串所指定的数据库的链接。服务器

###简单的输入检测 1.函数

ac=request.QueryString("ac")
msg=" 注册错误信息 "

在前端有这样的代码post

<%
=request.QueryString("msg")	
%>
<form name="form1" method="post" action="addnewdata.asp?ac=adduser">

第一个部分会输出msg所含的错误信息ui

第二部分action="addnewdata.asp?ac=adduser会传递一个名为ac的参数给服务器端界面。code

if request.form("username")="" then
	msg=msg&"<br>"&" 用户名不能为空 "
end if

request.form(不是from!):获得从按post提交方式的表单中name为"username"的数据 若是为空:msg自加一个换行符和"用户名不能为空";以后两个if意思相同。

if strcomp(msg," 注册错误信息 ")>0 then
	response.Redirect("reg.asp?msg="&msg)
end if

strcomp:字符串比较函数,若是msg发生变更,response.Redirect(重定向)到前端注册页面,并将错误信息以参数形式传递给注册页面。

###检测用户名是否被注册 1.

set rsc=server.createobject("adodb.recordset")

建立一个recordset(记录集)对象的示例。

sql="select * from user where username ='"&request.form("username")&"'"

指定数据源的sql语句,赋值给"sql"变量。

rsc.open sql,conn,1,1

recordset的open方法:open source,activeconnection,cursortype,locktype, options

source:指定recordset对象的数据源(通常是sql语句);

activeconnection: 指定recordset对象使用的连接(通常为connection对象实例);

curtype:游标类型;

locktype:锁定类型。

if rsc.eof and rsc.bof then
else
ck=rsc("username")
end if

第一行意为:若是rsc在第一条记录以前也是最后一条记录以后,即rsc不存在数据,则什么也不作,(为了不记录集为空时会出现错误)。

第三行:将rsc中username的记录赋值给ck;

if ck<>"" then
    msg=msg&"<br>"&" 用户名被人注册 "
    response.Redirect("reg.asp?msg="&msg)
end if

若是ck不为空:即用户名已使用,则msg自加"用户名已被注册"错误信息。 重定向到前端登录界面。

###添加新的用户信息 1.

dsql="select * from user where id is null"
set rs=server.createobject("adodb.recordset")
rs.open dsql,conn,1,3

选择一条id为空的记录集,这里id是一个自增的列。

rs.addnew
rs("username")=request.form("username")
rs("password")=request.form("password")
rs("mail")=request.form("mail")
rs("sex")=request.form("qq")
rs("address")=request.form("add")
rs("ntime")=now
rs.update

rs.addnew与rs.update配合使用,添加并更新这些数据。

set rs=nothing

释放recordset实例。

附上前端(reg.asp)代码:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
	<title>用户注册</title>
</head>
<body>
<center>
	用户注册<br>
<%
=request.QueryString("msg")	
%>
<form name="form1" method="post" action="addnewdata.asp?ac=adduser">
	<table width="39%" height="105" border="0">
		<tr>
			<td width="27%" height="30">用户名:</td>
			<td width="73%" height="30"><input type="text" name="username" id="username"></td>
		</tr>
		<tr>
			<td height="30">密码:</td>
			<td height="30"><input type="password" name="password" id="password">
			</td>
		</tr>
		<tr>
			<td height="30">肯定密码:</td>
			<td height="30"><input type="password" name="password2" id="password2"></td>
		</tr>
		<tr>
			<td height="30">性别:</td>
			<td height="30"><input type="text" name="sex" id="sex"></td>
		</tr>
		<tr>
			<td height="30">QQ:</td>
			<td height="30"><input type="text" name="qq" id="qq"></td>
		</tr>
		<tr>
			<td height="30">Mail:</td>
			<td height="30"><input type="text" name="mail" id="mail"></td>
		</tr>
		<tr>
			<td height="30">地址:</td>
			<td height="30"><input type="text" name="add" id="add"></td>
		</tr>
		<tr>
		<td> </td>
		<td><input type="submit" name="Submit" value="提交"></td>
	</table>
</form>
</center>
</body>
</html>
相关文章
相关标签/搜索