求助一个投票过程的日志记录顺序如何修改?
判断投票时有的项目的选项没有选择就跳回投票页面,但是刚投的空票却将投票的iP和时间记录到数据库里面了。要如何才能先判断再记录呢?
代码如下:
<%option explicit%>
<%response.expires = 0%>
<!--#include file= "common/conn.asp "-->
<%
'验证投票日期是否有效
'startTime 所指那天为投票开始的第一天
'endTime 所指那天为投票结束的第一天
'endTimeValid 结束时间是否有效
'投票还没开始返回 1
'投票已经结束返回 -1
'投票时间有效返回 0
function ValidVoteDate(startTime, endTime, endTimeValid)
if DateDiff( "d ", startTime, Date()) < 0 then
ValidVoteDate = 1
elseif endTimeValid and DateDiff( "d ", Date(), endTime) <= 0 then
ValidVoteDate = -1
else
ValidVoteDate = 0
end if
end function
'验证投票间隔是否有效
function ValidVoteInterval(conn, rs, subjectID, clientIP, intervalValue, intervalUnit)
dim sql
sql = "select top 1 logTime from voteLogs where subjectID= " & CLng(subjectID) & " and clientIP= ' " & Replace(clientIP, " ' ", " ' ' ") & " ' order by logID desc "
rs.Open sql, conn, 1, 1
if not rs.eof then
if DateDiff(intervalUnit, rs( "logTime "), Now()) > intervalValue then
ValidVoteInterval = true
else
ValidVoteInterval = false
end if
else
'记录不存在,些 IP 还没有针对本主题进行过投票
ValidVoteInterval = true
end if
rs.Close
end function
'为一项投票
'仅被 UpdateOptions 调用
function UpdateOption(conn, itemID, validOptionsCnt)
dim votedOptionsCnt
votedOptionsCnt = request.Form( "option_ "&itemID).count
if validOptionsCnt> 0 and votedOptionsCnt> validOptionsCnt then
'validOptionsCnt <=0 表示不限制选项数
exit function
elseif votedOptionsCnt=0 then
response.Write( "未完成项目选项选择!请重新选择! ")
response.Write( " <a href= " "# " " onclick= " "javascript:window.close();return false " "> 关闭窗口 </a> ")
exit function
response.end
end if
dim i, optionIDStr
for i=1 to votedOptionsCnt
optionIDStr = optionIDStr & ", " & CLng(request.Form( "option_ "&itemID)(i))
next
dim sql, answer
if Len(optionIDStr) > 1 then
sql = "update options set optionValue=optionValue+1 where itemID= " & CLng(itemID) & " and optionID in ( " & Mid(optionIDStr, 2) & ") "
conn.Execute sql
end if
end function
'设置投票记录
sub SetVoteInterval(conn, subjectID, clientIP, yijian)
dim sql, aaaa
aaaa = request.Form( "answer ")
sql = "insert into voteLogs(subjectID, clientIP, logTime, yijian) values( " & CLng(subjectID) & ", ' " & Replace(clientIP, " ' ", " ' ' ") & " ', Now(), ' "&aaaa& " ') "
conn.Execute sql
end sub
'为各项投票
sub UpdateOptions(conn, rs, subjectID)
dim sql, itemIDArr(), validOptionsCntArr(), i
sql = "select itemID, validOptionsCnt from items where subjectID= " & CLng(subjectID)
rs.Open sql, conn, 1, 1
redim itemIDArr(rs.recordCount-1)
redim validOptionsCntArr(rs.recordCount-1)
i = 0
do while not rs.eof
itemIDArr(i) = rs( "itemID ")
validOptionsCntArr(i) = rs( "validOptionsCnt ")
rs.MoveNext
i = i + 1
loop
rs.Close
for i=0 to UBound(itemIDArr)
call UpdateOption(conn, itemIDArr(i), validOptionsCntArr(i))
next
end sub
'返回 boolean 类型,以表示是否需要显示“结果跳转”
function Vote(conn, rs)
Vote = true
dim sql
dim subjectID, clientIP, yijian
dim startTime, endTime, endTimeValid, intervalValue, intervalUnit
subjectID = CLng(request.Form( "subjectID "))
if subjectID <= 0 then
Vote = false
response.Write( "缺少 subjectID 参数。 ")
exit function
end if
g_subjectID = subjectID
clientIP = request.serverVariables( "REMOTE_ADDR ")
cIP = clientIP
sql = "select startTime, endTime, endTimeValid, intervalValue, intervalUnit, securityRslt from subjects where subjectID= " & subjectID
rs.Open sql, conn, 1, 1
if not rs.eof then
startTime = rs( "startTime ")
endTime = rs( "endTime ")
endTimeValid = rs( "endTimeValid ")
intervalValue = rs( "intervalValue ")
intervalUnit = rs( "intervalUnit ")
Vote = not rs( "securityRslt ")
rs.Close
else
rs.Close
Vote = false
response.Write( "参数 subjectID 对应的记录不存在。 ")
exit function
end if
dim vvd
vvd = ValidVoteDate(startTime, endTime, endTimeValid)
if vvd = 1 then
response.Write( "投票日期还没有到。 ")
exit function
elseif vvd = -1 then
response.Write( "投票日期已过。 ")
exit function
end if
if not ValidVoteInterval(conn, rs, subjectID, clientIP, intervalValue, intervalUnit) then
if intervalUnit = "h " then
response.Write( "同一 IP 对本主题的投票时间间隔应该在 " & intervalValue & " 小时以上。 ")
response.Write( " <a href= " "# " " onclick= " "javascript:window.close();return false " "> 关闭窗口 </a> ")
response.end
exit function
elseif intervalUnit = "n " then
response.Write( "同一 IP 对本主题的投票时间间隔应该在 " & intervalValue & " 分钟以上。 ")
response.Write( " <a href= " "# " " onclick= " "javascript:window.close();return false " "> 关闭窗口 </a> ")
response.end
exit function
else
response.Write( "此 IP 已经为本主题进行了投票,如果不是您所投的,请稍候再试。 ")
response.Write( " <a href= " "# " " onclick= " "javascript:window.close();return false " "> 关闭窗口 </a> ")
response.end
exit function
end if
end if
call SetVoteInterval(conn, subjectID, clientIP, yijian)
call UpdateOptions(conn, rs, subjectID)
response.Write( "投票完成。 </p> ")
end function
%>
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> 处理投票 </title>
<style type= "text/css ">
<!--
.STYLE1 {
color: #FF0000;
font-size: 12px;
}
.z {
font-size: 12px;
}
a:link {
color: #0066CC;
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #993300;
}
a:hover {
text-decoration: underline;
color: #FF6600;
}
a:active {
text-decoration: none;
}
-->
</style>
</head>
<body class= "z ">
<p>
<%
call DatabaseStart( "common/ ")
dim g_subjectID,cIP
if Vote(conn, rs) then
%>
<span class= "STYLE1 "> 参加调查者资料: </span> </p>
<p> 参加调查的IP: <%=cIP%> </p>
<p> 参加调查时间: <%=now()%> </p>
<p> <span class= "STYLE1 "> (此资料将作为抽奖的依据,可将其记下) </span> </p>
<p> <a href= "vote_rslt.asp?subjectID= <%=g_subjectID%> "> 点击此处查看调查结果 </a>
<%
else
response.Write( " <a href= " "# " " onclick= " "javascript:window.close();return false " "> 关闭窗口 </a> ")
end if
call DatabaseEnd()
%>
</p>
</body>
</html>
[解决办法]
function UpdateOption(conn, itemID, validOptionsCnt)
dim votedOptionsCnt
votedOptionsCnt = request.Form( "option_ "&itemID).count
if validOptionsCnt> 0 and votedOptionsCnt> validOptionsCnt then
'validOptionsCnt <=0 表示不限制选项数
exit function
elseif votedOptionsCnt=0 then
response.Write( "未完成项目选项选择!请重新选择! ")
response.Write( " <a href= " "# " " onclick= " "javascript:window.close();return false " "> 关闭窗口 </a> ")
exit function
response.end
end if
dim i, optionIDStr
for i=1 to votedOptionsCnt
optionIDStr = optionIDStr & ", " & CLng(request.Form( "option_ "&itemID)(i))
next
dim sql, answer
if Len(optionIDStr) > 1 then
sql = "update options set optionValue=optionValue+1 where itemID= " & CLng(itemID) & " and optionID in ( " & Mid(optionIDStr, 2) & ") "
conn.Execute sql
end if
end function
----------------------------
if validOptionsCnt> 0 and votedOptionsCnt> validOptionsCnt then
这句是验证吗?
---------------------------
太长了 再添加到数据库前验证一下就好了?
不符合就返回,用javascript返回上一页
是这个问题吗?