我正在建立一個在線投票系統。我需要檢查是否定義了某個cookie。如果是不是,那麼增加投票。否則,什麼都不要做。問題與投票系統,Cookie問題
我只是使用非常簡單的代碼。任何人都可以解釋我錯過了什麼?
代碼投票:
<cfprocessingdirective suppresswhitespace="yes">
<cfsetting showdebugoutput="no" enablecfoutputonly="yes">
<cfoutput>
<cfset user_post_type = "#trim(form.vote)#">
<cfset unique_content_id = IIf(IsDefined("FORM.unique_id") AND Int(FORM.unique_id), Int(FORM.unique_id), DE("0"))>
<cfset unique_content_id = Hash(unique_content_id)>
<cfswitch expression="#user_post_type#">
<cfcase value="up">
<cfif isDefined('cookie.votes_#unique_content_id#')>
<!--- Do Nothing or show an alert --->
<cfelse>
	<cfquery name="matchVotes" datasource="#application.cw.dsn#" username="#application.cw.dsnUsername#"
password="#application.cw.dsnPassword#">
select vote_up from voting_count where unique_content_id = '#val(form.unique_id)#' limit 1
</cfquery>
<cfif matchVotes.recordcount>
<cfquery name="updmatchVotes" datasource="#application.cw.dsn#" username="#application.cw.dsnUsername#"
password="#application.cw.dsnPassword#">
update voting_count set vote_up = vote_up+1
where unique_content_id = '#val(form.unique_id)#'
</cfquery>
<cfelse>
<cfquery name="insertmatchVotes" datasource="#application.cw.dsn#" username="#application.cw.dsnUsername#"
password="#application.cw.dsnPassword#">
insert into voting_count(unique_content_id,vote_up)
VALUES('#val(form.unique_id)#',1)
</cfquery>
</cfif>
<cfcookie name="cookie.votes_#unique_content_id#" value="1" expires="#createTimeSpan(0,2,0,0)#">
</cfif>
<cfquery name="getVotes" datasource="#application.cw.dsn#" username="#application.cw.dsnUsername#"
password="#application.cw.dsnPassword#">
select vote_up from voting_count where unique_content_id = '#val(form.unique_id)#' limit 1
</cfquery>
<cfset total = getVotes.vote_up>
<cfoutput>#total#</cfoutput>
</cfcase>
</cfswitch>
</cfoutput>
<cfsetting enablecfoutputonly="no">
</cfprocessingdirective>
您不需要將cookie設置爲cookie的前綴,因此它變成了cookie.cookie – Busches 2013-05-02 11:56:17
您是否收到錯誤?它怎麼不起作用?我們需要更多細節。 – 2013-05-02 12:01:27
如有疑問,請查看您的數據。看看我的答案在這裏==> http://stackoverflow.com/questions/16320945/having-some-trouble-understanding-loops <==並將其應用於您的情況。 – 2013-05-02 14:08:17