看上去很美 之asp

本文分析了一段ASP代码中关于用户注册的逻辑实现,并指出了其中存在的问题。通过逐步注释和修改,揭示了可能导致的问题及其原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看上去很美

论坛上有位MM发贴问了一个问题,真是惭愧,我一眼看上去就发现了一个比较明显的错误,估计可能是她的笔误.我刚回完贴不久,她用消息跟我联系了!错误依旧

原贴:

http://topic.youkuaiyun.com/u/20080609/13/ab9951e1-6195-4513-a619-b0e304af7c5a.html

先瞒着MM,没有征得人家的同意,转载人家的代码:)

<% 

set rst=server.createobject("ADODB.recordset") 

rst.open "select * from users where u_user='"& request.form("f_user") & "'",conn,1,1 

if rst.recordcount>0 then 

response.write "用户名:"&request("f_user")&"已存在,请击后退按钮返回上一页!" 

response.end 

rst.close 

set rst=nothing 

conn.close 

set conn=nothing 

else 

rst.open "users",conn,1,3 

rst.addnew 

rst("u_user")=request("f_user") 

rst("u_code")=request("f_code") 

rst.update 

rst.close 

set rst=nothing 

conn.clsoe 

set conn=nothing 

end if 

%>

我们一起来分析一下!

1.先用传过来的用户名查询是否存在!如果存在就提示,反之就注册.看上去很美

2.既然存在两个业务我们就一个一个看.

set rst=server.createobject("ADODB.recordset") 

rst.open "select * from users where u_user='"& request.form("f_user") & "'",conn,1,1 

' if rst.recordcount>0 then 

response.write "用户名:"&request("f_user")&"已存在,请击后退按钮返回上一页!" 

response.end 

rst.close 

set rst=nothing 

conn.close 

set conn=nothing 

现在假设它只执行一个业务,所以if哪行我注释掉了,没有问题我们就继续看一下业务

 

<% 

set rst=server.createobject("ADODB.recordset") 

rst.open "select * from users where u_user='"& request.form("f_user") & "'",conn,1,1 

'if rst.recordcount>0 then 

if 1 <> 1 then

response.write "用户名:"&request("f_user")&"已存在,请击后退按钮返回上一页!" 

response.end 

rst.close 

set rst=nothing 

conn.close 

set conn=nothing 

else 

rst.open "users",conn,1,3 

rst.addnew 

rst("u_user")=request("f_user") 

rst("u_code")=request("f_code") 

rst.update 

rst.close 

set rst=nothing 

conn.clsoe 

set conn=nothing 

end if 

%>

注释掉原来的if判断,我们用一个类似java的断言来短路代码,让执行流程跳到else块上执行.如果你看着还不是很清晰的话可以像上面哪样把它(if哪段代码)删掉,像这样:

<% 

set rst=server.createobject("ADODB.recordset") 

rst.open "select * from users where u_user='"& request.form("f_user") & "'",conn,1,1 



'此处是删除的部分占位



rst.open "users",conn,1,3 

rst.addnew 

rst("u_user")=request("f_user") 

rst("u_code")=request("f_code") 

rst.update 

rst.close 

set rst=nothing 

conn.clsoe 

set conn=nothing 

end if 

%>

现在,只要稍微写过点asp的人都能发现一个错误了:

rst.open "select * from users where u_user='"& request.form("f_user") & "'",conn,1,1 

rst.open "users",conn,1,3 

这两行执行完后(或执行不完时),会发生什么呢?

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值