疯了,if和else同时执行了,怎么办?
以下是我的代码,在调试中发现if和else同时执行了。请大侠解释一下:
<!--#include virtual= "/Public/Conn.asp "-->
<%
dim userName
dim userTypeID,Pwd,SafeEmail,LinkMan,Sex,Phone
dim sql,k
dim s
userTypeID = Trim(Request.Form( "userTypeID "))
Pwd = Trim(Request.Form( "Pwd "))
SafeEmail = Trim(Request.Form( "SafeEmail "))
LinkMan = Trim(Request.Form( "LinkMan "))
Sex = Trim(Request.Form( "Sex "))
Phone = Trim(Request.Form( "Phone "))
userName = Trim(request.Form( "Name "))
sql= "select [Name] from userinfo where [Name] = ' " & userName & " ' "
Set k = Server.CreateObject( "Adodb.Recordset ")
k.open sql,cn,1,3
If k.eof and k.bof Then
sql= "insert into UserInfo(userTypeID,Name,Pwd,SafeEmail,LinkMan,Sex,Phone) values( "&userTypeID& ", ' "&userName& " ', ' "&Pwd& " ', ' "&SafeEmail& " ', ' "&LinkMan& " ', ' "&Sex& " ', ' "&Phone& " ') "
cn.Execute sql
Set s = cn.Execute( "select * from userinfo where ID = (SELECT MAX(ID) FROM UserInfo) ")
If Not (s.EOF And s.bof) Then
session( "username ") = s( "Name ")
session( "userInfoID ") = s( "ID ")
session( "userTypeID ") = s( "userTypeID ")
End If
else
Response.Write " <script language= 'javascript '> "
Response.Write " alert( '此用户名已存在! '); "
Response.Write " history.back(); "
Response.Write " </script> "
response.End()
end if
k.close
set k=nothing
cn.Close
%>
用户名在数据库明明是不存在的,他执行的时候,好像先插入了数据,然后又提示永明已经存在,真不知道是怎么回事?
------解决方案--------------------If k.eof and k.bof Then 换别的试试呢
If k.eof or k.bof Then
If k.eof Then
If not k.eof Then ---- 反一反试试
------解决方案--------------------能否调试一下看看几时候插入数据的阿.
------解决方案--------------------还有是否程序执行了两遍啊?
------解决方案--------------------sql= "insert into UserInfo(userTypeID,Name,Pwd,SafeEmail,LinkMan,Sex,Phone) values( "&userTypeID& ", ' "&userName& " ', ' "&Pwd& " ', ' "&SafeEmail& " ', ' "&LinkMan& " ', ' "&Sex& " ', ' "&Phone& " ') "
cn.Execute sql
换成 rs.addnew 方式 试试
------解决方案--------------------你反过来写吧。
If not(k.eof or err) Then
err
else
addnew
end if
------解决方案--------------------在else前加上response.end()