Skip navigation
Currently Being Moderated

Oh functions, how I loathe thee...

Nov 3, 2011 11:11 AM

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?

 
Replies
  • Currently Being Moderated
    Nov 3, 2011 11:23 AM   in reply to BreakawayPaul

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 3, 2011 11:43 AM   in reply to BreakawayPaul

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 3, 2011 11:45 AM   in reply to BreakawayPaul

    Regarding:

    Then I changed the db from date/time to text

     

    Change it back before you forget.  Storing dates and times as text is a bad idea.

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 3, 2011 11:48 AM   in reply to BreakawayPaul

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 3, 2011 11:51 AM   in reply to BreakawayPaul

    Then I changed the db from date/time to text

     

    If the column stores dates - leave it as a date/time.

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 3, 2011 12:19 PM   in reply to BreakawayPaul

    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>

     

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 3, 2011 12:24 PM   in reply to BreakawayPaul

    >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

     

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 3, 2011 12:33 PM   in reply to BreakawayPaul

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 3, 2011 12:37 PM   in reply to BreakawayPaul

    <cfqueryparam cfsqltype="cf_sql_datetime" value="#now()#">

     

    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 ...

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 3, 2011 12:38 PM   in reply to -==cfSearching==-

    Ah, good catch. Thanks for the correction Leigh

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 3, 2011 12:42 PM   in reply to BreakawayPaul

    EDIT: Actually, the error message has changed:

    Data type mismatch in criteria expression. 

     

    Did you remember to change the data type of "timestamp" back to datetime?

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 3, 2011 12:51 PM   in reply to BreakawayPaul

    Great news!

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 3, 2011 12:58 PM   in reply to BreakawayPaul

    Glad you got it worked out.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points