I have a form that submits to another page, and on the target page I'm trying to add a function to log the activity.
So I did this:
Target page:
<cfinvoke component="actionlog" method="additem">
<cfinvokeargument name="details" value="Edited user: #FORM.email#">
</cfinvoke>
And the cfc:
<cfcomponent>
<cffunction name="additem" access="public">
<cfargument name="details" type="string" required="yes">
<cfquery name="addlog" datasource="hepoffice">
INSERT INTO actions
( timestamp
, user
, action )
VALUES
( '#DateFormat(Now(), "yyyy-mm-dd|hh:mm:ss")#'
, '#GetAuthUser()#'
, <cfqueryparam value="#arguments.details#" cfsqltype="cf_sql_varchar"> )
</cfquery>
</cffunction>
</cfcomponent>
I've done this sort of thing in the past, and it's worked, but not I'm getting an error:
Error Executing Database Query.
Syntax error in INSERT INTO statement.
The error occurred in D:\Inetpub\staffnet_test\hep\admin\actionlog.cfc: line 12
Called from D:\Inetpub\staffnet_test\hep\admin\hepadmin.cfm: line 87
Called from D:\Inetpub\staffnet_test\hep\admin\hepadmin.cfm: line 75
Called from D:\Inetpub\staffnet_test\hep\admin\hepadmin.cfm: line 64
Called from D:\Inetpub\staffnet_test\hep\admin\hepadmin.cfm: line 1
Called from D:\Inetpub\staffnet_test\hep\admin\hepadmin.cfm: line 1
Called from D:\Inetpub\staffnet_test\hep\admin\actionlog.cfc: line 12
Called from D:\Inetpub\staffnet_test\hep\admin\hepadmin.cfm: line 87
Called from D:\Inetpub\staffnet_test\hep\admin\hepadmin.cfm: line 75
Called from D:\Inetpub\staffnet_test\hep\admin\hepadmin.cfm: line 64
Called from D:\Inetpub\staffnet_test\hep\admin\hepadmin.cfm: line 1
Called from D:\Inetpub\staffnet_test\hep\admin\hepadmin.cfm: line 1
10 : ( '#DateFormat(Now(), "yyyy-mm-dd|hh:mm:ss")#'
11 : , '#GetAuthUser()#'
12 : , <cfqueryparam value="#arguments.details#" cfsqltype="cf_sql_varchar"> )
13 : </cfquery>
14 : </cffunction>
What am I doing wrong?
turning on robust debugging (SQL) when encountering these sort of errors will give you a better idea of what went wrong - especially the part where it generates the SQL statement as text so you can double check your syntax. Does date format support a time mask? I thought you needed to use TimeFormat for that, but it might be different in CF9
Hope that helps,
-Michael
If GetAuthUser returns a string, you need to quote it in your query. Better yet, use cfqueryparam to escape apostrophes in that string.
Also, there are better ways to do the timestamp. I believe the best is to use the database function that gives you the current timestamp. If you insist on using ColdFusion for that, don't format now(), just send it. Now() returns a timestamp. Use cfqueryparam for that as well.
INSERT INTO actions ( timestamp, user, action )
I would bet the problem is one or more of your column names is a reserved word in your database. TIMESTAMP and USER are likely suspects. In which case your options are to rename the columns or escape them (syntax is database specific). (BTW: It is always a good thing to mention your database type when queries are involved)
> '#DateFormat(Now(), "yyyy-mm-dd|hh:mm:ss")#'
> Does date format support a time mask?
s Micheal mentioned, DateFormat does not fully support time masks. Not surprising given the function name. But be aware "m" only represents month number, not minutes. That said, if "timestamp" is a datetime column it is best to use datetime objects, not strings. Lose the quotes and dateFormat() and just insert #now()#
-Leigh
The reserved words was something I should have thought of, because I had that exact problem a few weeks ago.
I changed the column names and the query to match. No dice.
I changed the timestamp to just use #now()#, the wrapped each query item in cfqueryparam. Still no dice.
I'm stumped!
Does this work:
<cfcomponent>
<cffunction name="additem" access="public">
<cfargument name="details" type="string" required="yes">
<cfquery name="addlog" datasource="hepoffice">
INSERT INTO [actions]
( [timestamp]
, [user]
, [action] )
VALUES
( <cfqueryparam cfsqltype="cf_sql_datetime" value="#now()#">
, <cfqueryparam value="#GetAuthUser()#" cfsqltype="cf_sql_varchar">
, <cfqueryparam value="#arguments.details#" cfsqltype="cf_sql_varchar"> )</cfquery>
</cffunction>
</cfcomponent>
>I changed the column names and the query to match.
I am still betting on reserved word issues. Which column(s) did you change and what is your current query? (And you still did not tell us which database are you using. Hint, hint...)
I changed the timestamp to just use #now()#, the wrapped each query item in cfqueryparam. Still no dice.
It would not. Using a date string would not that error. It is just a bad practice.
Leigh
Nope. It seems to be hung up on the last line, where it plugs in the details. It's like the details aren't making it to the function.
I tried pulling the cfinvoke and pasting the query there, but got the same error.
EDIT: Actually, the error message has changed:
Data type mismatch in criteria expression.
Odd since the column type is text and I'm using varchar.
Unfortunately, I'm stuck using Access for the db.
Unfortunately, I'm stuck using Access for the db.
Okay, then Timestamp and User are almost certainly reserved words. So Michael's suggestion of using brackets Re: Oh functions, how I loathe thee... should do the trick. (Though renaming is preferable.) If that does not work, can you post your current cfquery?
BTW: The correct cfsqltype is cf_sql_timestamp. There is no cf_sql_datetime. (We can probably thank the jdbc specs for that ..) Though it would certainly be more intuitive ...
That did the trick! It works now! Woohoo!
Oh and FYI, the current column names are action_time, user_name, and action_done.
Thanks everyone!
North America
Europe, Middle East and Africa
Asia Pacific