如何将查询中的值设置为输入文本框
How to set value from a query to an input textbox
当用户单击 table 中特定行的编辑时,我希望同一页面中的输入文本框填充该行的详细信息。我试过了,但没用。
<cfquery name="getDataForEdit" datasource="dsn">
select * from usercardetails where id = '#url.id#'
</cfquery>
<cfoutput>
<cfset #form.username# = #getDataForEdit.username#>
</cfoutput>
以这个为例让你继续......
<cfquery name="getDataForEdit" datasource="dsn">
select * from usercardetails where id = '#url.id#'
</cfquery>
<!---
OPTIONAL IMPROVEMENT: You might change your SQL to use CFQueryParam, this will protect against SQL injection
select * from usercardetails where id = <cfqueryparam cfsqltype="CF_SQL_NUMERIC" value="#url.id#" />
--->
<!---
OPTIONAL FOR DEBUGGING: If you uncomment this block it will show you the results of your query.
<cfoutput><cfdump var="#getDataForEdit#" label="getDataForEdit" expand="No" /><br /></cfoutput>
--->
<cfoutput>
<cfif getDataForEdit.recordcount is 1> <!--- Check that you only get one row back in results --->
<!--- Coldfusion will build forms for you using cfform, but many coders keep away from it, so instead you need to build the HTML of your form yourself. --->
<form action="index.cfm" method="post">
<p><label for="username">Username</label><input type="text" id="username" name="username" value="#getDataForEdit.username#" /></p>
<p><input type="submit" id="butty01" name="butty01" value="Go" /></p>
</form>
<cfelseif getDataForEdit.recordcount is 0> <!--- If no results --->
<p>No records found</p>
<cfelse> <!--- Anything else will mean many results --->
<p>An error occurred (Multiple records with this ID found)</p>
</cfif>
</cfoutput>
我在代码中添加了一些可选改进的注释。
另请注意...
<cfset #form.username# = #getDataForEdit.username#>
会给你带来麻烦。
<cfset username = getDataForEdit.username>
将 create/set 变量用户名等于您查询中用户名列的值。 # 标签在此上下文中不是必需的。 # 打印出变量的值,所以你可以这样做...
<cfset username = "#getDataForEdit.username#">
这会将用户名列的值打印成一个文本字符串(字符串中唯一的东西就是值),并且文本字符串将被分配给变量用户名。
form
是一个保留字,因为它是一个结构(值得查找结构),其中包含发布到您页面的所有表单变量数据。您可以使用 cfdump
检查表单
<cfoutput><cfdump var="#form#" label="form" expand="No" /><br /></cfoutput>
要打印出表单内的值之一,您可以这样做
#form.username#
所以(当你是初学者时会很困惑)...
<cfset #form.username# = #getDataForEdit.username#>
将创建一个变量,其名称与发布到您页面的表单字段用户名的值相对应,并用您的查询中的用户名填充它。你真的不想要那个。
坚持下去。一旦你掌握了一些基本概念,Coldfusion 就是一种可爱的编码语言。
当用户单击 table 中特定行的编辑时,我希望同一页面中的输入文本框填充该行的详细信息。我试过了,但没用。
<cfquery name="getDataForEdit" datasource="dsn">
select * from usercardetails where id = '#url.id#'
</cfquery>
<cfoutput>
<cfset #form.username# = #getDataForEdit.username#>
</cfoutput>
以这个为例让你继续......
<cfquery name="getDataForEdit" datasource="dsn">
select * from usercardetails where id = '#url.id#'
</cfquery>
<!---
OPTIONAL IMPROVEMENT: You might change your SQL to use CFQueryParam, this will protect against SQL injection
select * from usercardetails where id = <cfqueryparam cfsqltype="CF_SQL_NUMERIC" value="#url.id#" />
--->
<!---
OPTIONAL FOR DEBUGGING: If you uncomment this block it will show you the results of your query.
<cfoutput><cfdump var="#getDataForEdit#" label="getDataForEdit" expand="No" /><br /></cfoutput>
--->
<cfoutput>
<cfif getDataForEdit.recordcount is 1> <!--- Check that you only get one row back in results --->
<!--- Coldfusion will build forms for you using cfform, but many coders keep away from it, so instead you need to build the HTML of your form yourself. --->
<form action="index.cfm" method="post">
<p><label for="username">Username</label><input type="text" id="username" name="username" value="#getDataForEdit.username#" /></p>
<p><input type="submit" id="butty01" name="butty01" value="Go" /></p>
</form>
<cfelseif getDataForEdit.recordcount is 0> <!--- If no results --->
<p>No records found</p>
<cfelse> <!--- Anything else will mean many results --->
<p>An error occurred (Multiple records with this ID found)</p>
</cfif>
</cfoutput>
我在代码中添加了一些可选改进的注释。
另请注意...
<cfset #form.username# = #getDataForEdit.username#>
会给你带来麻烦。
<cfset username = getDataForEdit.username>
将 create/set 变量用户名等于您查询中用户名列的值。 # 标签在此上下文中不是必需的。 # 打印出变量的值,所以你可以这样做...
<cfset username = "#getDataForEdit.username#">
这会将用户名列的值打印成一个文本字符串(字符串中唯一的东西就是值),并且文本字符串将被分配给变量用户名。
form
是一个保留字,因为它是一个结构(值得查找结构),其中包含发布到您页面的所有表单变量数据。您可以使用 cfdump
检查表单<cfoutput><cfdump var="#form#" label="form" expand="No" /><br /></cfoutput>
要打印出表单内的值之一,您可以这样做
#form.username#
所以(当你是初学者时会很困惑)...
<cfset #form.username# = #getDataForEdit.username#>
将创建一个变量,其名称与发布到您页面的表单字段用户名的值相对应,并用您的查询中的用户名填充它。你真的不想要那个。
坚持下去。一旦你掌握了一些基本概念,Coldfusion 就是一种可爱的编码语言。