看上去很美
論壇上有位MM發貼問了一個問題,真是慚愧,我一眼看上去就發現了一個比較明顯的錯誤,估計可能是她的筆誤.我剛回完貼不久,她用訊息跟我聯絡了!錯誤依舊
原貼:
http://topic.csdn.net/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 thenresponse.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
這兩行執行完後(或執行不完時),會發生什麼呢?