Inserting the Data
June 28, 2006 | 0 comments
Inserting the Data
In this step, you set up the document to insert data into the database. This is the document referenced in your form action in the JavaScript.
- With Dreamweaver open, create a ColdFusion template named insert.cfm in the same folder as your SWF and HTML file.
- Switch to Code view.
- Create a
cfquery
tag. - Enter myQuiz for the
datasource
attribute. You defined the data source in the previous section. - Enter insertData for the name attribute, to name your query.
- After the query in the body, type a thank you message (see below).
Copy and paste the following code snippet into the insert.cfm file:
<!--This query inserts the test scores into the database.
The INSERT statement specifies the column names.
The VALUES statement specifies the values to insert.
Note that this query uses CFIF statements to determine whether
the field contains a value or is empty (or NULL). -->
<cfquery datasource="myQuiz" name="insertData">
INSERT INTO Captivate (total, correct, accuracy, name, email)
VALUES (
<cfif IsDefined("FORM.Total") AND #FORM.Total# NEQ "">
'#FORM.Total#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.correct") AND #FORM.correct# NEQ "">
#FORM.correct#
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.Accuracy") AND #FORM.Accuracy# NEQ "">
'#FORM.Accuracy#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.Name") AND #FORM.Name# NEQ "">
'#FORM.Name#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.Email") AND #FORM.Email# NEQ "">
'#FORM.Email#'
<cfelse>
NULL
</cfif>
)
</cfquery>
<html>
<head>
<title>Quiz Results</title>
<meta http-equiv="refresh" content="3;URL=javascript:window.close();">
</head>
<body>
Thank you for participating in this survey. <br />
This window will close automatically.
</body>
</html>
Now, you have created a way to insert information from the quiz into your Microsoft Access database. Test the CFML file with the following steps:
- Browse your HTML file in your browser (the URL you will browse your file at will be similar to: http://localhost:8500/Captivate_scoring/myquiz.htm. (Your URL may be different based on where you placed your files in your directory structure and your host server port.)
- Take the quiz several times. When the scoring window appears, enter your username and e-mail address and click Submit Your Score. This will enter data into your database. Repeat this step several times by refreshing the HTML file in your browser.
- Open your database to see the scoring results.
Comments