Question

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?

Was it helpful?

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>

OTHER TIPS

I think I may have solved it... forgot the quotes, and they're both varchar fields :-/

<cfloop collection="#results#" item="ID" >
    <cfquery name="insertStuff" datasource="myDataSource">
        INSERT INTO web..Stuff (ID, Name)
        VALUES ('#ID#', '#results[ID]#')
    </cfquery>
</cfloop>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top