DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   ColdFusion (http://www.daniweb.com/forums/forum19.html)
-   -   <cfquery> Undefined (http://www.daniweb.com/forums/thread115938.html)

zoid777 Mar 27th, 2008 8:41 am
<cfquery> Undefined
 
I need some help with <cfquery>, I want to authenticate an add form so the user doesn't enter the same value twice or more so I used recordcount like this

  1. <cfquery name="cool.recordcount" datasource="#Request.proto#">
  2. <cfif cool EQ 0>
  3. INSERT INTO proton(
  4. name,
  5. moon
  6. )
  7. VALUES(
  8. '#Trim(Form.name)#',
  9. '#Trim(Form.moon)#',
  10. )
  11. <cfif cool EQ 0>
  12. <cfelse>
  13. THIS RECORD ALREADY EXIST
  14. <cfinclude template="adduser.cfm">
  15. </cfif>
  16. </cfquery>

but it throws an error like Variable COOL is undefined. I am also using anplication.cfm Application.cfm also in my form, is their anything you can see I am doing wrong or do I have to define the query name cool, it is already defined in the cfquery?

cmhampton Mar 28th, 2008 11:55 am
Re: <cfquery> Undefined
 
First of all, please don't name your query cool.recordCount. RecordCount is a property of the query object. Name it something like qryCool.

Secondly, you're missing a </cfif> tag. You have two cfif statements, but only one closing.

Do you already have code that determines if this is a duplicate record? What is the 'cool' variable for? If you already check, try this code and let me know:

  1. <cfif cool EQ 0>
  2.   <cfquery name="qryCool" datasource="#Request.proto#">
  3.     INSERT INTO proton(
  4.       name,
  5.       moon
  6.     )
  7.     VALUES(
  8.       '#Trim(Form.name)#',
  9.       '#Trim(Form.moon)#'
  10.     )
  11.   </cfquery>
  12. <cfelse>
  13.   This Record Already Exists
  14.   <cfinclude template="adduser.cfm">
  15. </cfif>

Change the adduser.cfm to include it's own <cfquery> tag.

Alternately, you can do it all in SQL if you haven't determined whether this is a duplicate record or not.

  1.  
  2.   <cfquery name="qryCool" datasource="#Request.proto#">
  3.     IF NOT EXISTS
  4.       (
  5.         SELECT
  6.           name,
  7.           moon
  8.         FROM
  9.           proton
  10.         WHERE
  11.           name =  '#Trim(Form.name)#'
  12.           AND moon =  '#Trim(Form.moon)#'
  13.       )
  14.       BEGIN
  15.         INSERT INTO proton(
  16.           name,
  17.           moon
  18.         )
  19.         VALUES(
  20.           '#Trim(Form.name)#',
  21.           '#Trim(Form.moon)#'
  22.         )
  23.  
  24.       SELECT SCOPE_IDENTITY() AS RecordID
  25.  
  26.       END
  27.  
  28.     ELSE
  29.       SELECT NULL AS RecordID
  30.   </cfquery>
  31.  
  32.   <cfif qryCool.RecordID EQ "">
  33.     This Record Already Exists
  34.   <cfelse>
  35.     <!--- Do something here --->
  36.   </cfif>

zoid777 Mar 29th, 2008 2:57 am
Re: <cfquery> Undefined
 
Thanks cmhampton the Direct SQL version worked, I just modified the cfif at the buttom and it works.


All times are GMT -4. The time now is 4:58 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC