Data is selected from one table and inserted into another table. This data happens to be key to first table, yet foreign key to the second table. Using cfquery
, select and insert works without any issues. But when this data happens to contain more than one space, for example "XXX ZZZ"
(there are three space in the middle) I will get a foreign key error when inserting data. Using db2, the error is -530.
I created a record with 2 spaces, same error. Created one space, with no error. This seem to prove that when inserting with cfquery, ColdFusion keeps one space, and removes any others if they exist.
I even try to use replace(data," "," ","all")
or chr(32)
to replace the space, but still no luck passing more than one space and inserting successfully.
Hope you guys could help on solving this. Thanks!
<cfquery name="GetCategories" datasource="#DbSource#" username = "#DbUserId#" password = "#DbUserPswd#">
SELECT category, rtrim(category) as tcategory
FROM role_category
ORDER BY category
FOR FETCH ONLY
</cfquery>
Populate dropdown, tried following 3 ways, neither works
1
<select name="category" id="category" size = "4" onChange="someFunction();">
<cfoutput query="GetCategories">
<option value ="#category#" >#category#</option>
</cfoutput>
</select>
2
<select name="category" id="category" size = "4" onChange="someFunction();">
<cfoutput query="GetCategories">
<cfset cat = replace(GetCategories.category," "," ","all")>
<option value ="#cat#" >#cat#</option>
</cfoutput>
</select>
3
<select name="category" id="category" size = "4" onChange="someFunction();">
<cfoutput query="GetCategories">
<cfset tcat = replace(GetCategories.tcategory," "," ","all")>
<option value ="#tcat#" >#tcat#</option>
</cfoutput>
</select>
in the action file:
<cfquery name="InsertUserRole" datasource="#DbSource#" username = "#DbUserId#" password = "#DbUserPswd#">
INSERT INTO role(role,desc, category)
VALUES ('#_role#','#form.desc#','#form.category#')
</cfquery>
The error message from Coldfusion is : DB2 SQL Error: SQLCODE=-530, SQLSTATE=23503, SQLERRMC=CATEGORY1, DRIVER=3.63.123
Update from comments:
I tried adding this, but that did not do it:
<cfqueryparam cfsqltype="cf_sql_char" value="#form.category#">