在coldfusion函数中使用sql查询的正确方法是什么

at0kjp5o  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(417)

我有一个将变量传递给函数并插入它的代码。但我有个错误:

<cffunction name="insertSupplierPersonnel" output="false" access="public" returnType="struct">
    <cfargument name="name" type="string" required="true" />
    <cfargument name="email" type="string" required="false" default="" />
    <cfargument name="office_phone" type="string" required="false" default="" />
    <cfargument name="mobile_phone" type="string" required="false" default="" />
    <cfargument name="designation" type="string" required="false" default="" />

    <cfset var res = '' />

    <cfquery datasource="#session.dsn_aset#" result="res">
        INSERT INTO `supplier_personnel_incharge` (
            `name`,
            `email`,
            `office_phone`,
            `mobile_phone`,
            `designation`
        )
        VALUES
        (
            cfargument.name,
            cfargument.email,
            cfargument.office_phone,
            cfargument.mobile_phone,
            cfargument.designation
        ) ;
    </cfquery>

    <cfreturn res />
</cffunction>

<cfset res = insertSupplierPersonnel(name='#form.personnel_name#', email='#form.personnel_email#', office_phone='#form.personnel_office_phone#', mobile_phone='#form.personnel_mobile_phone#', designation='#form.personnel_designation#') />

<cfdump  var="#res#">

我得到这个错误:

有个问题 cfargument.name . 正确的使用方法是什么 cfargument 是否插入查询?提前谢谢。

szqfcxe2

szqfcxe21#

首先,正确的范围是 arguments ,不是 cfargument . 所以,改变这种情况:

cfargument.name,

对此:

arguments.name,

接下来,必须用磅号将变量名括起来,以获得变量的值,即。 #arguments.name# .
接下来,使用查询参数,即。 <cfqueryparam value="#arguments.name#"> . 除此之外,它们将转义sql查询语法中使用的特殊字符。

sc4hvdpw

sc4hvdpw2#

总结以上所有正确答案和评论。这将是您的最佳实践:
功能 returnType 应该是“query”,而不是“struct”
如果您指定 default 值,cf将参数识别为“不需要”
使用 cfqueryparam 在所有查询参数上
可选择的
使用 null 的属性 cfqueryparam 插入 NULL 如果没有给出值
在sql语句的末尾不需要尾随分号

<!---return type is query, not struct --->
<cffunction name="insertSupplierPersonnel" output="false" access="public" returnType="query">
    <cfargument name="name" type="string" required="true" />
    <!--- NOTE: If you specify a default value, CF recognizes the argument as "not required" --->
    <cfargument name="email" type="string" default="" />
    <cfargument name="office_phone" type="string" default="" />
    <cfargument name="mobile_phone" type="string" default="" />
    <cfargument name="designation" type="string" default="" />

    <cfquery datasource="#session.dsn_aset#" result="local.data">
        INSERT INTO supplier_personnel_incharge (
            name, /*Unless your database column names are case-sensitive, you don't need quotation marks around the column names*/
            email,
            office_phone,
            mobile_phone,
            designation
        )
        VALUES
        (
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#trim(arguments.name)#">,
            /*insert NULL if there is no value given*/
            <cfqueryparam cfsqltype="cf_sql_varchar" null="#Not Len(trim(arguments.email))#" value="#trim(arguments.email)#">,
            <cfqueryparam cfsqltype="cf_sql_varchar" null="#Not Len(trim(arguments.office_phone))#" value="#trim(arguments.office_phone)#">,
            <cfqueryparam cfsqltype="cf_sql_varchar" null="#Not Len(trim(arguments.mobile_phone))#" value="#trim(arguments.mobile_phone)#">,
            <cfqueryparam cfsqltype="cf_sql_varchar" null="#Not Len(trim(arguments.designation))#" value="#trim(arguments.designation)#">,
        ) /*you don't need a trailing semi-colon*/
    </cfquery>

    <cfreturn local.data />
</cffunction>

<cfset local.res = insertSupplierPersonnel(name='#form.personnel_name#',
    email='#form.personnel_email#', 
    office_phone='#form.personnel_office_phone#', 
    mobile_phone='#form.personnel_mobile_phone#', 
    designation='#form.personnel_designation#') />

<cfdump var="#local.res#">

相关问题