Coldfusion #iif 和会话

发布于 2024-10-31 15:53:51 字数 757 浏览 5 评论 0原文

我正在构建一个添加/编辑用户表单,当访问页面时,有一个 if 语句检测我们是否正在编辑当前用户或通过 url id 添加新用户

<cfif isDefined('URL.id')>
    <cfquery name="getSquadMember" datasource="#application.datasource#">
        SELECT * from squad WHERE id=#URL.id#
    </cfquery>
    <cfset #name#=#getSquadMember.athlete_name# />
<cfelse>
    <cfset SESSION.squad = structNew()>
    <cfparam name="SESSION.squad.name" default="">
</cfif>

这很好,但问题在于评估会话是否或者值存在,我收到错误

<cfinput  class="text" name="name" type="text" id="name" value ="#IIf(IsDefined('name'), DE('#name#'), DE("#SESSION.squad.name#"))#" required="yes"  />

Elementquad.name 在会话中未定义。为什么在满足第一个条件时评估会话是否存在?

谢谢,

R。

I am building an add/edit user form, when the page is accessed there is an if statement which detects if we are editing a current user or adding a new user by a url id

<cfif isDefined('URL.id')>
    <cfquery name="getSquadMember" datasource="#application.datasource#">
        SELECT * from squad WHERE id=#URL.id#
    </cfquery>
    <cfset #name#=#getSquadMember.athlete_name# />
<cfelse>
    <cfset SESSION.squad = structNew()>
    <cfparam name="SESSION.squad.name" default="">
</cfif>

That is fine but the problem comes in evaluting if the session or value exists, I get an error

<cfinput  class="text" name="name" type="text" id="name" value ="#IIf(IsDefined('name'), DE('#name#'), DE("#SESSION.squad.name#"))#" required="yes"  />

Element squad.name is undefined in session. Why is it evaluating if session exists when the first condition is met?

Thanks,

R.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

微暖i 2024-11-07 15:53:51

首先 - 如果您可以避免使用 iif(),那么就这样做。它引起的麻烦比它解决的问题还要多……

但是,请注意 ColdFusion 文档中有关 iif() 的以下内容:

如果变量未定义,ColdFusion 在处理该函数时会抛出错误。下面的例子展示了这个问题:

#IIf(IsDefined("Form.Deliver"), DE(Form.Deliver), DE("no"))# 这将返回“解析参数 FORM.DELIVER 时出错”。

要避免此问题,请在代码中使用 DE 和 Evaluate 函数,如下所示:

#IIf(IsDefined("Form.Deliver"), Evaluate(DE("Form.Deliver")), DE("no"))# 这将返回“no”; ColdFusion 不会抛出错误。

或者,如果您使用的是 CF9(支持三元运算符):

First up - if you can possibly avoid using iif(), then do so. It causes many more headaches than it has ever solved...

However, note the following from the ColdFusion documentation on iif():

If a variable is undefined, ColdFusion throws an error when it processes this function. The following example shows this problem:

#IIf(IsDefined("Form.Deliver"), DE(Form.Deliver), DE("no"))# This returns "Error resolving parameter FORM.DELIVER".

To avoid this problem, use the DE and Evaluate functions in code such as the following:

#IIf(IsDefined("Form.Deliver"), Evaluate(DE("Form.Deliver")), DE("no"))# This returns "no"; ColdFusion does not throw an error.

Or, if you're on CF9 (which supports ternary operators):

<cfinput class="text" name="name" type="text" id="name" value ="#IsDefined('name') ? name : SESSION.squad.name#" required="yes" />

满栀 2024-11-07 15:53:51

为什么同一个项目需要两个不同的变量名称,是否有原因?你能这样做吗:

<cfif isDefined('URL.id')>

<cfquery name="getSquadMember" datasource="#application.datasource#">
SELECT * from squad WHERE id=<cfqueryparam value="#URL.id#" cfsqltype="CF_SQL_INTEGER" />
</cfquery>
<cfset name= getSquadMember.athlete_name />

<cfelse>
<cfset squad = structNew()>
<cfset name = "" />
</cfif>
<input type="text" name="name" id="name=" value="#name#" />

这将反过来消除对 iff 语句

HTH

J的需要

Is there a reason as to why you need to have two different variable names for the same item? Could you just do:

<cfif isDefined('URL.id')>

<cfquery name="getSquadMember" datasource="#application.datasource#">
SELECT * from squad WHERE id=<cfqueryparam value="#URL.id#" cfsqltype="CF_SQL_INTEGER" />
</cfquery>
<cfset name= getSquadMember.athlete_name />

<cfelse>
<cfset squad = structNew()>
<cfset name = "" />
</cfif>
<input type="text" name="name" id="name=" value="#name#" />

Which would then in turn remove the need for the iff statement

HTH

J

迷荒 2024-11-07 15:53:51

有几个问题:

1)

<cfquery name="getSquadMember" datasource="#application.datasource#">
SELECT * from squad WHERE id=#URL.id#
</cfquery>

您在这里请求 SQL 注入。使用 cfqueryparam

2)

<cfset #name#=#getSquadMember.athlete_name# />

奇怪

这是你的第二篇文章,我注意到 #它应该像 Jason 写的那样

<cfset name = getSquadMember.athlete_name />

: 3)至于你的问题:

<cfinput class="text" name="name" type="text" id="name" value="#IIf(IsDefined('name'), DE('#name#'), DE("#SESSION.squad.name#"))#" required="yes" />

这种方式并不是真正可读。
首先为该值设置一些变量。
第二件事 - 您不需要像这样将变量放入 DE 中,这样就可以了:

DE(name)

并且当您使用 cfinput 时,整个事情可能需要正确。还有另一个问题 - 你真的不需要在那里使用 cfinput 。普通的 HTML 输入就可以了,并且可以为您节省一些 CF 解析。

Couple of issues:

1)

<cfquery name="getSquadMember" datasource="#application.datasource#">
SELECT * from squad WHERE id=#URL.id#
</cfquery>

You're asking for a SQL injection here. Use cfqueryparam

2)

<cfset #name#=#getSquadMember.athlete_name# />

It's your second post where I notice weird use of #

It should be as Jason wrote it:

<cfset name = getSquadMember.athlete_name />

3) As for your question:

<cfinput class="text" name="name" type="text" id="name" value="#IIf(IsDefined('name'), DE('#name#'), DE("#SESSION.squad.name#"))#" required="yes" />

This way is not really readable.
Set some variable for the value first.
Second thing - you don't need to put the variables in DE like this, this will do:

DE(name)

And as you're using cfinput the whole thing probably needs to be correct. And another issue - you don't really need to use cfinput there. Normal HTML input will do and save you some CF parsing.

你的往事 2024-11-07 15:53:51

您看到的问题是 IIF() 必须评估语句的所有部分。
您不能真正使用它来检查变量是否已定义,因为它总是尝试评估 true 和 false 响应的内容。

只要在使用 IIF() 时牢记这一点,使用它就没有问题。只要保持简单,当变量可能不存在时不要尝试使用它。

我本来打算指出您的代码的其他问题,但其他答案已经很好地涵盖了这些问题。

The problem you are seeing is that IIF() has to evaluate all parts of the statement.
You can't really use it for checking if a variable is defined or not as it will always try to evaluate the contents of both the true and false responses.

As long as you bear this in mind when using IIF() there's no problem with using it. Just keep it simple and don't try to use it when a variable might not exist.

I was going to point out the other issues with your code, but the other answers have already done a good job of covering these.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文