利用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>
相關文章
相關標籤/搜索