I have a simple update query, only one table involved. I first wrote this without using CFQUERYPARAM and kept getting errors when the integer fields (zip,plus 4, etc) were null. So, I rewrote using CFQUERYPARAM so that the null values didn't produce the errors. Now, when I enter something into the integer fields, the data does not get saved.
What am I missing?
Thanks
DW
<cfquery name="updt_person" datasource="#application.datasource#">
  UPDATE tblperson 
  SET 
    firstname = '#form.firstname#', 
    lastname = '#form.lastname#', 
    address_line_1 = '#form.address_line_1#', 
    address_line_2 = '#form.address_line_2#', 
    city = '#form.city#', 
    stateid = #form.stateid#, 
    zip = <cfqueryparam value = "#form.zip#" cfsqltype = "CF_SQL_INTEGER" null = "yes">, 
    plus4 = <cfqueryparam value = "#form.plus4#" cfsqltype = "CF_SQL_INTEGER" null = "yes">, 
    area_code = <cfqueryparam value = "#form.area_code#" cfsqltype = "CF_SQL_INTEGER" null = "yes">, 
    prefix = <cfqueryparam value = "#form.prefix#" cfsqltype = "CF_SQL_INTEGER" null = "yes">, 
    suffix = <cfqueryparam value = "#form.suffix#" cfsqltype = "CF_SQL_INTEGER" null = "yes"> 
  WHERE personid = #get_personid.personid#
</cfquery>
				
                        
First thing first. Please use
cfqueryparam, to all user inputs when you use it in a query. The fields#form.firstname#, #form.lastname#, etcall should be in acfqueryparamto prevent SQL Injection.The issue you are facing here is the wrong use of
NULLattribute of thecfqueryparamtag.The
nullparam should be an expression which resultstrueorfalse. If you provideyesas the value directly, then the result becomes like this.suffix = NULLNow, let us see how to use
nullattribute.The above will make sure
NULLis passed as the column value if theform.suffixis blank. You can change this validation based on your application logic.Also, newer versions (CF 11+) does not require the
CF_SQL_prefix in thetypeattribute.So the final query should look something like this.