Search code examples
coldfusioninsertcfquery

What's wrong with my simple insert?


I'm using coldfusion to insert the contents of a struct (key-value pairs) into a database table. This is my code:

<cfloop collection="#results#" item="ID" >
    <cfquery name="insertStuff" datasource="myDataSource">
        INSERT INTO web..Stuff (ID, Name)
        VALUES (#ID#, #results[ID]#)
    </cfquery>
</cfloop>

This seems simple enough... but I'm getting the following error:

Incorrect syntax near 'VA'. 

Any ideas?


Solution

  • You really ought to think about parameterising your data too.

    <cfloop collection="#results#" item="ID" >
        <cfquery name="insertStuff" datasource="myDataSource">
            INSERT INTO web..Stuff (ID, Name)
            VALUES (
                <cfqueryparam cfsqltype="cf_sql_varchar" value="#ID#">, 
                <cfqueryparam cfsqltype="cf_sql_varchar" value="#results[ID]#">)
        </cfquery>
    </cfloop>