Published on: Thursday 8th April 1999 By: Andrew Shatwell
The code in this file runs on the server with no output back to the user's browser. It takes the contents of the form submitted in postnew.cfm and adds it to the database.
<CFQUERY NAME="GetID" DATASOURCE="#MyDatabase#"> SELECT Max([ThreadID]) AS high FROM tblThreads; </CFQUERY>
The first query examines the tblThreads table and returns the highest value of the ThreadID field. The SQL statement defines this as high and it is used in the next two queries.
<CFQUERY NAME="NewThread" DATASOURCE="#MyDatabase#"> INSERT INTO tblThreads (GroupID, ThreadID, ThreadName, Username<CFIF isDefined("notify")>, Distribution</CFIF>) VALUES (#GroupID#,(#GetID.high# + 1),'#ThreadName#','#UserName#'<CFIF isDefined("notify")>,'#UserEmail#'</CFIF>) </CFQUERY>
The next query is a very basic example of dynamic query. It is a normal INSERT query with one exception. If the user has clicked the "Notify me by email" checkbox on the form in postnew.cfm then an extra field is specified in the query.
The ability to dynamically construct queries based on the responses of the user can be incredibly powerful. In partnership with the CFFORM tag which will allow you to insert information into the database without having to hard-code field names, Cold Fusion provides you with a very flexible platform which you can tailor uniquely to the user.
isDefined is a Cold Fusion function that returns TRUE if the parameter passed to it is a defined variable, or FALSE if it isn't.
The value assigned to a checkbox is only submitted with the form if the checkbox has been selected. When submitting empty checkboxes, it is important to realise that as opposed to receiving a field with a null value, we do not receive the field at all.
<CFQUERY NAME="NewArticle" DATASOURCE="#MyDatabase#"> INSERT INTO tblArticles (GroupID, ThreadID, ThreadName, [Link], Posted, UserName, UserEmail, Message) VALUES (#GroupID#,(#GetID.high# + 1),'#ThreadName#', (#GetID.high# + 1), #Posted#, '#UserName#', '#UserEmail#', '#Message#') </CFQUERY>
This query adds the user's message to the tblArticles table. Having calculated the highest value of ThreadID in the first query, incrementing it by one ensures that the message is posted into a new thread.
The CFLOCATION tag is used for directing the server to another page. Upon the script's completion the server is directed to reload the openforum.cfm page.