Exporting data to Word with Cold Fusion
How to update/edit data using Cold Fusion
You are here: irt.org | Articles | Database | My First Forum - a bulletin board application [ previous next ]
Published on: Thursday 8th April 1999 By: Andrew Shatwell
This script contains a form that submits the results to itself. This being the case, the first half of the script actually gets run after the second half!
<CFIF #isDefined("Fwho")#> <CFQUERY NAME="login" DATASOURCE="#MyDatabase#"> SELECT FirstName, UserName,LastLogin FROM Users WHERE Username = '#Fwho#' </CFQUERY> <html> <head> <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache"> <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <title>My First Forum</title> </head> <body BGCOLOR="#FFFFFF" BACKGROUND="../images/gridpaperbackground2.gif"> <div align="center"> <IMG SRC="../images/logo2.gif" BORDER="0" ALT="My First Forum" width="469" height="64"> <p> <CFIF #login.recordcount# eq 1> <CFCOOKIE NAME="Fwho" VALUE="#Fwho#"> <font color="#0000ff"><b>Login successful</b></font> <p><b>Good <CFIF hour(now()) gt 0 and hour(now()) lt 12>Morning<CFELSE>Afternoon</CFIF> <CFOUTPUT>#login.Firstname#</CFOUTPUT></b> <p><b>You can now view the restricted bulletin boards.</b> <p>Click <a href="board.cfm">here</a> to proceed. <p><br><font size="1" face="Verdana, Arial">Last login on: <CFOUTPUT>#dateformat(login.LastLogin,"dd-mm-yy")#</CFOUTPUT></font> <CFQUERY NAME="UpdateLogin" DATASOURCE="#MyDatabase#"> UPDATE Users SET LastLogin = #now()# WHERE Username = '#Fwho#' </CFQUERY> <CFABORT> <CFELSE> <font color="#ff0000"><b>Login failed</b></font> <CFABORT> </div></body></html> </CFIF> </CFIF>
The initial CFIF statement checks for the existence of Fwho. This is a field on the form and will be present if the user has just tried to log in.
The login query retrieves all the records from the Users table that have a Username that matches the value the user just entered.
The next CFIF statement checks the number of records returned by the query. If no records were returned then the user has entered an invalid login name. A message saying "Login failed" is displayed and the script is ended by the CFABORT tag.
If the query returns a record then this means the user has logged in successfully. We use CFCOOKIE to store the value of Fwho in a cookie. No expiration date is set for the cookie so it will be deleted once the user closes their browser.
Next, a message is displayed telling the user they have logged in along with the date and time they last logged in. The UpdateLogin query enters the current date and time in the "last logged in" field.
<html> <head> <title>My First Forum</title> </head> <body bgcolor="#ffffff" BACKGROUND="../images/gridpaperbackground2.gif" onLoad="document.form1.Fwho.focus()"> <div align="center"> <IMG SRC="../images/logo2.gif" BORDER="0" ALT="My First Forum" width="469" height="64"> <p> <form name="form1" method="post" action="login.cfm"> <table width="75%" cellpadding="0" cellspacing="0"> <tr><td><FONT face="Verdana, Arial" color="#000080">Certain forums may only be viewed by authorised users. <p>Click <a href="mailto:admin@myforum.com">here</a> to request a User ID or login below.</font><p><br></td></tr> <tr><td align="center"> <b>Enter your User ID:</b> <input type="password" name="Fwho" size="10" maxlength="10"> <input type="submit" value="Login"> </td></tr> </table> </form> </body> </html>
This half of the script is the first one processed. It is simply a form that requests the user to enter a UserID. Upon pressing the Submit button, the contents of the form are posted to the login.cfm file (this file) and the first half of the file processes the information.
<html> <head> <title>My First Forum</title> </head> <CFIF #isDefined("adminpass")#> <CFIF adminpass eq "admin"> <!--- This is only a example file - a secure registration screen was not my intent! ---> <CFIF not isDefined("UserName")> <html> <body bgcolor="#ffffff" BACKGROUND="../images/gridpaperbackground2.gif"> <div align="center"> <IMG SRC="../images/logo2.gif" BORDER="0" ALT="My First Forum" width="469" height="64"> <p> <form name="form1" action="admin.cfm" method="post"> <CFOUTPUT><input type="hidden" name="adminpass" value="#adminpass#"></CFOUTPUT> <table cellpadding="3" cellspacing="1" border="0"> <tr><td><font face="Verdana, Arial" color="#000080"><b>BoaT ID:</b> </font></td> <td><font size="-1"><input type="text" name="UserName" maxlength="10" size="10"></font></td></tr> <tr><td><font face="Verdana, Arial" color="#000080"><b>FirstName:</b> </font></td> <td><font size="-1"><input type="text" name="FirstName" maxlength="20" size="20"></font></td></tr> <tr><td><font face="Verdana, Arial" color="#000080"><b>Surname:</b> </font></td> <td><font size="-1"><input type="text" name="Surname" maxlength="20" size="20"></font></td></tr> <tr><td><font face="Verdana, Arial" color="#000080"><b>Email:</b> </font></td> <td><font size="-1"><input type="text" name="Email" maxlength="50" size="25"></font></td></tr> <tr><td><font face="Verdana, Arial" color="#000080"><b>Access:</b> </font></td> <td><font size="-1"><select name="Access"><option selected>Admin<option>Beta<option>Public</select></font></td></tr> <tr><td colspan="2" align="center"><input type="submit" value="Create User"></td></tr> </table> </form> <font face="Verdana, Arial"><b>Make sure all fields are completed</b></font> </div> </body> </html> <CFABORT> <CFELSE> <CFQUERY NAME="Add" DATASOURCE="#MyDatabase#"> INSERT INTO Users (UserName, FirstName, Surname, Email, Access, Password) VALUES ('#form.UserName#', '#form.FirstName#', '#form.Surname#', '#form.email#', '#form.Access#', '#form.FirstName#') </CFQUERY> <body bgcolor="#ffffff" BACKGROUND="../images/gridpaperbackground2.gif"> <h2>User added successfully</h2> </body> </html> <CFABORT> </CFIF> </CFIF> </CFIF> <body bgcolor="#ffffff" BACKGROUND="../images/gridpaperbackground2.gif" onLoad="document.form1.adminpass.focus()"> <div align="center"> <IMG SRC="../images/logo2.gif" BORDER="0" ALT="My First Forum" width="469" height="64"> <p> <form name="form1" method="post" action="admin.cfm"> <b>Enter password</b> <input type="password" name="adminpass" size="10" maxlength="10"> </form> </body> </html>
The admin.cfm file can perform three tasks, depending on which parameters are also loaded with the file. If the file is loaded normally, as opposed to being the target of a form submission, it asks for a password. This password is stored in the adminpass field.
Submitting this form causes the file to be reloaded. This time the condition in the first CFIF statement will be TRUE. Assuming the user entered the correct password, we proceed past the second CFIF statement. The Username field has not been defined yet and so the user is presented with another form.
After entering the details of the new user, the contents of this form are submitted to the admin.cfm file again. The value of adminpass is contained in a hidden field in this form.
The third time this file is loaded both adminpass and Username are defined so the server runs the SQL between the CFQUERY tags to INSERT the user's details into the database.
Back to Cancelling a message - part 2
Exporting data to Word with Cold Fusion
How to update/edit data using Cold Fusion