今天弄一个网站,要向数据库里添加数据,不知道为什么就不能添加数据,代码如下:<% set rs=server.CreateObject("adodb.recordset") %>
<% if request("action")="add" then
if len(request("password"))<6 then
response.Write("密码不能小于6位!")
response.End
else
name1=request.Form("name")
pwd=md5(request.Form("password"))
pro=request.Form("quanxian")
set rs=server.CreateObject("adodb.recordset")
sql="select * from admin where admin='"&name1&"'"
rs.open sql,conn,1,3
end if
if not rs.eof then
'response.Write "该用户名已经存在"
'response.Write"<br>"
'response.End
response.write "<script language='javascript'>" & chr(13)
response.write "alert('该用户已经存在,请更换其他用户名!');" & Chr(13)
response.write "window.document.location.href='adminadd.asp';"&Chr(13)
response.write "</script>" & Chr(13)
Response.End
else
rs.addnew
rs("admin")=name1
rs("password")=pwd
rs("pro")=pro
rs("roleid")=1
rs("time")=now()
rs.update
'response.Write "添加管理员成功!!"
'Response.Write "<a href='adminmanage.asp'>"
'Response.Write "返回"
'Response.Write "</a>"
'response.End
response.write "<script language='javascript'>" & chr(13)
response.write "alert('添加管理员成功!');" & Chr(13)
response.write "window.document.location.href='adminmanage.asp';"&Chr(13)
response.write "</script>" & Chr(13)
Response.End
end if
end if
%>
<% if request("action")="add" then
if len(request("password"))<6 then
response.Write("密码不能小于6位!")
response.End
else
name1=request.Form("name")
pwd=md5(request.Form("password"))
pro=request.Form("quanxian")
set rs=server.CreateObject("adodb.recordset")
sql="select * from admin where admin='"&name1&"'"
rs.open sql,conn,1,3
end if
if not rs.eof then
'response.Write "该用户名已经存在"
'response.Write"<br>"
'response.End
response.write "<script language='javascript'>" & chr(13)
response.write "alert('该用户已经存在,请更换其他用户名!');" & Chr(13)
response.write "window.document.location.href='adminadd.asp';"&Chr(13)
response.write "</script>" & Chr(13)
Response.End
else
rs.addnew
rs("admin")=name1
rs("password")=pwd
rs("pro")=pro
rs("roleid")=1
rs("time")=now()
rs.update
'response.Write "添加管理员成功!!"
'Response.Write "<a href='adminmanage.asp'>"
'Response.Write "返回"
'Response.Write "</a>"
'response.End
response.write "<script language='javascript'>" & chr(13)
response.write "alert('添加管理员成功!');" & Chr(13)
response.write "window.document.location.href='adminmanage.asp';"&Chr(13)
response.write "</script>" & Chr(13)
Response.End
end if
end if
%>
看了好久,不知道怎么回事,最后想这个代码是我自己图省事,从以前的代码里弄过来的,是不是代码出错了呢,因此又换了代码,自己有写了一个,呵呵,成功了,真不知道怎么回事??
新代码如下:username=request.form("name")
password=request.form("password")
pro=request.form("quanxian")
roleid=1
dim rs,rsa
set rsa=conn.execute("select * from admin where admin='"&username&"' order by id desc")
if not rsa.eof then
response.Write("<script> alert('用户名已存在,请更换其他用户名!');this.location.href='adminadd.asp';</script>")
else
set rs=conn.execute("insert into admin(admin,password,pro,roleid) values('"&username&"','"&password&"','"&pro&"','"&roleid&"')")
end if
end if
password=request.form("password")
pro=request.form("quanxian")
roleid=1
dim rs,rsa
set rsa=conn.execute("select * from admin where admin='"&username&"' order by id desc")
if not rsa.eof then
response.Write("<script> alert('用户名已存在,请更换其他用户名!');this.location.href='adminadd.asp';</script>")
else
set rs=conn.execute("insert into admin(admin,password,pro,roleid) values('"&username&"','"&password&"','"&pro&"','"&roleid&"')")
end if
end if
成功添加数据 ok了
转载于:https://blog.51cto.com/mypan/150362