Skip to main content
Inspiring
May 14, 2006
Answered

CFC question

  • May 14, 2006
  • 3 replies
  • 425 views
The register cfc should check that the username in the form doesn`t already exist and if it doesn`t then insert the user into the db. The result will be returned to the index.cfm page where the users is either sent to another page or sees a message syaing that the users already exists. I have had a good crack at it but it doesn`t seem to be doing anything. Thanks for any advice

register.cfc
<cfcomponent>
<cffunction access="public" name="adduser" output="0">
<!--- add new user function --->
<!--- username and password required --->
<cfargument name="cfcUsername" type="string" required="1" />
<cfargument name="cfcPassword" type="string" required="1" />

<cfset variables.verificationKey = CreateUUID()>

<!--- query the SecurityDB for the passed username --->
<cfquery name="checkusername" datasource="SecurityDB" username="root" password="riveravon">
SELECT username
FROM Security
WHERE username = '#arguments.cfcUsername#'
</cfquery>

<!--- if the username already exists then return that result--->
<cfif checkusername.recordCount>
<cfreturn checkusername.recordCount />

<cfelse>
<!--- the username doesn`t exist so enter user´s details and return that --->
<cfquery name="newUser" datasource="SecurityDB" username="root" password="riveravon">
INSERT INTO security (username, password, activationcode)
VALUES ('#form.username#', '#form.password#', '#variables.verificationKey#')
</cfquery>

<cfreturn checkusername.recordCount />
</cfif>
</cffunction>
</cfcomponent>


index.cfm
<cfif structKeyExists(form,"adduser")>
<!--- The user pressed the register insert button --->
<cfinvoke
component="register"
method="adduser"
returnVariable="adduser"
cfcUsername="#form.username#">
<cfset adduser="#adduser#">
</cfif>

<form name="adduser" method="post" action="index.cfm">
<DIV align=center>Username:<INPUT id=username name=username>
<br>
Password:
<INPUT id=password name=password>
</DIV>
<input tabindex="3" type="image" name="adduser" src="images/register.gif" height=43 width=164 border="0"></A></DIV>
</form>

<cfif isDefined("variables.adduser")>
<cfif variables.adduser NEQ 0>
<b>This Username already exists! Please enter another</b>
<cfelse>
<cflocation url="activation.cfm" addtoken="No">
</cfif>
</cfif>
This topic has been closed for replies.
Correct answer Dan_Bracuk
At first glance, your code looks ok. I suggest you put some cfdumps in your adduser function right after the cfif and cfelse tags to help you troubleshoot.

3 replies

Inspiring
May 14, 2006
Thanks for the replies Dan and Mamdoh.
I enabled Robust Exception Information in the cf administrator.

I changed the code and realized that I get an error for some reason with an image as the submit button. I changed it. I now get the following error which is what you were saying the CFCPASSWORD is not passed. Why not? Thanks:

The parameter CFCPASSWORD to function adduser is required but was not passed in.


The error occurred in C:\CFusionMX7\wwwroot\CFIDE\REGISTERING\index.cfm: line 27

25 : method="adduser"
26 : returnVariable="adduser"
27 : cfcUsername="#form.username#">
28 : cfcPassword="#form.password#">
29 : <cfset adduser="#adduser#">

register.cfc
<cfcomponent>
<cffunction access="public" name="adduser" output="0">
<!--- add new user function --->
<!--- username and password required --->
<cfargument name="cfcUsername" type="string" required="1" />
<cfargument name="cfcPassword" type="string" required="1" />

<cfset variables.verificationKey = CreateUUID()>

<!--- query the SecurityDB for the passed username --->
<cfquery name="checkusername" datasource="SecurityDB" username="root" password="riveravon">
SELECT username
FROM Security
WHERE username = '#arguments.cfcUsername#'
</cfquery>


<cfif checkusername.recordCount>
<cfreturn checkusername.recordCount />
<cfelse>
<cfquery name="newUser" datasource="SecurityDB" username="root" password="riveravon">
INSERT INTO security (username, password, activationcode)
VALUES ('#form.username#', '#form.password#', '#variables.verificationKey#')
</cfquery>

<cfreturn 0 />
</cfif>
</cffunction>
</cfcomponent>

index.cfm
<cfif structKeyExists(form,"adduser")>
<!--- The user pressed the register insert button --->
<cfinvoke
component="register"
method="adduser"
returnVariable="adduser"
cfcUsername="#form.username#">
cfcPassword="#form.password#">
<cfset adduser="#adduser#">
</cfif>

<cfdump var = "#variables#">
<cfif isDefined("variables.adduser")>
<cfif variables.adduser NEQ 0>
<b>This Username already exists! Please enter another</b>
<cfelse>
<cflocation url="activation.cfm" addtoken="No">
</cfif>
</cfif>

<form name="adduser" method="post" action="index.cfm">
<DIV align=center>Email:
<INPUT class=camporegister name=username>
<br>
Password:
<INPUT class=camporegister name=password></DIV>
<input tabindex="3" type="Submit" name="adduser" class="ftforminputsmall">
</DIV></form>

<!--- this image as a submit button causes errors so changed it to above--->
<!---<input tabindex="3" type="image" name="adduser" src="images/register.gif" height=43 width=164 border="0">--->
May 14, 2006
Hydrowizard

I noticed that the cfinvoke tag is missing one variable which is cfcPassword. Also, I will suggest to revise your code to be like this

<!--- if the username already exists then return that result--->
<cfif not isDefined("checkusername.recordCount")>

<!--- the username doesn`t exist so enter user´s details and return that >
<cfquery name="newUser" datasource="SecurityDB" username="root" password="riveravon">
INSERT INTO security (username, password, activationcode)
VALUES ('#form.username#', '#form.password#', '#variables.verificationKey#')
</cfquery>
<cfset checkusername.recordCount = 0/>

</cfif>
<cfreturn checkusername.recordCount />

OR

<cfif isDefined("checkusername.recordCount") and checkusername.recordCount>
<cfreturn 1 />

<cfelse>
<!--- the username doesn`t exist so enter user´s details and return that >
<cfquery name="newUser" datasource="SecurityDB" username="root" password="riveravon">
INSERT INTO security (username, password, activationcode)
VALUES ('#form.username#', '#form.password#', '#variables.verificationKey#')
</cfquery>

<cfreturn 0 />
</cfif>

Good Luck
Mamdoh Al-Habeeb
Dan_BracukCorrect answer
Inspiring
May 14, 2006
At first glance, your code looks ok. I suggest you put some cfdumps in your adduser function right after the cfif and cfelse tags to help you troubleshoot.