原理很简单,通过一句SQL语句:Select * from student where stname='"+stname+"' 接着就开始检查,当到(rs.eof)记录的最后一条还找不到时stname,说明这个名字(stname)没有被注册 则值行下面的动作,把他注删的信息进行添加。。。 详细代码如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ASP 防止重名注册</title>
<%
'数据库的连接
dim connstr
connstr="provider=microsoft.jet.oledb.4.0;data source=" + server.mappath ("db1.mdb")
set conn=server.createobject("adodb.connection")
conn.open connstr
%>
<%
if request("send")="ok" then
dim stname
stname = Request.Form("stname")
if stname = "" then
response.write "<mce:script type="text/javascript"><!--
alert('你没有输入用户名');
// --></mce:script>"
response.end
end if
sqlStr="Select * from student where stname='"+stname+"'"
Set rs=Server.CreateObject("Adodb.RecordSet")
rs.open sqlStr,conn,1,3
if not(rs.bof and rs.eof) then
Response.Write "<mce:script type="text/javascript"><!--
alert('您的用户名已被注册!');window.location.href('index.asp');
// --></mce:script>"
else
rs.Addnew
rs("stname")=Request.Form("stname")
rs.Update
Response.Write "<mce:script type="text/javascript"><!--
alert('注册成功!');window.location.href('index.asp');
// --></mce:script>"
end if
rs.close
set rs=nothing
response.end
end if
%>
</head>
<body>
<form id="form1" name="form1" method="post" action="index.asp">
<label>
<input name="stname" type="text" id="stname" value="" size="45" />
</label>
<label>
<input type="submit" name="button" id="button" value="提交" />
<input type="hidden" name="send" value="ok" />
</label>
</form>
</body>
</html>