Skip to main content
Inspiring
February 16, 2010
Answered

cfelseif not working

  • February 16, 2010
  • 3 replies
  • 1020 views

Hi,

Anyone have a suggestion as to why my cfelseif is not working?  I am trying to check to see if a username (login) and then a team name (teamname) is present so I don't get duplicates.  It checks the username (login), but forgets about the teamname.

<cfquery name="qlogin" datasource="mydb">
        SELECT * FROM team WHERE login LIKE <cfqueryparam value="%#form.login#%"> OR teamname LIKE <cfqueryparam value="%#form.login#%">
        </cfquery>   
           
        <cfif form.login IS #qlogin.login#>
       
        Username already taken, please <A HREF="javascript:history.back()">go back</A> and choose another username   
       
        <cfelseif form.teamname IS #qlogin.teamname#>
        Team Name already taken, please <A HREF="javascript:history.back()">go back</A> and choose another Team Name   
           
        <cfelse>

Insert this stuff into the database

</cfif>

    This topic has been closed for replies.
    Correct answer ilssac

    The usual way to do this type of multi-step checking is with a flag.

    <cfset insertFlag = true>

    <cfif form.login = query.login>

       Message

       <cfset insertFlag = false>

    </cfif>

    <cfif form.team = query.login>

      Message>

      <cfset insertFlag = false>

    </cfif>

    <!--- other checkes --->

    <cfif insertFlag eq true>

      insert records

    </cfif>

    P.S. Please note my other post concerning multiple records in your query record set.  You will probably want to make use of the valueList() function and  a listFind() function or some other method to check against multiple values.

    3 replies

    ilssac
    Inspiring
    February 16, 2010

    You are also going to have a problem that your query could, and probaly often will, return two records.

    The way your if statemens are writen, you will only ever check the first record in the result set.

    brianismAuthor
    Inspiring
    February 16, 2010

    Ian, would that be an issue though since there should only ever be 1 record if a record is present?  This is to check and make sure the database will never have a duplicate entry and tell the user they need to choose a different username or teamname.

    ilssac
    Inspiring
    February 16, 2010

    But when Joe, a new user, comes to your site and chooses a user name and a team name for himself.  Will it not be possible that he will choose a user name that User Bob picked and a team name that user Mary chose.  Thus your query will return two records, Bob's and Mary's.

    ilssac
    Inspiring
    February 16, 2010

    An elseIF clause is only checked if the previous if statement is false.

    So your team name is only going to be checked when the form login does not equal the query login.

    brianismAuthor
    Inspiring
    February 16, 2010

    Ian, Aha. I had a feeling that was the reason. Is there any easy way around this other than duplicating the actions? Reason being I have a lot more coding after where I have "Do the database inserts".  Example:

    <cfquery name="qlogin" datasource="mydb">
            SELECT * FROM team WHERE login LIKE <cfqueryparam value="%#form.login#%"> OR teamname LIKE <cfqueryparam value="%#form.login#%">
            </cfquery>   
               
            <cfif form.login IS #qlogin.login#>
           
            Username already taken, please <A HREF="javascript:history.back()">go back</A> and choose another username

    <cfelse>

    Do the database inserts

    </cfif>


            <cfif form.teamname IS #qlogin.teamname#>
            Team Name already taken, please <A HREF="javascript:history.back()">go back</A> and choose another Team Name   
               
            <cfelse>

    Do the database inserts

    </cfif>

    ilssac
    ilssacCorrect answer
    Inspiring
    February 16, 2010

    The usual way to do this type of multi-step checking is with a flag.

    <cfset insertFlag = true>

    <cfif form.login = query.login>

       Message

       <cfset insertFlag = false>

    </cfif>

    <cfif form.team = query.login>

      Message>

      <cfset insertFlag = false>

    </cfif>

    <!--- other checkes --->

    <cfif insertFlag eq true>

      insert records

    </cfif>

    P.S. Please note my other post concerning multiple records in your query record set.  You will probably want to make use of the valueList() function and  a listFind() function or some other method to check against multiple values.

    Participating Frequently
    February 16, 2010

    Are you looking for exact match or just like?

    <cfelseif form.teamname IS #qlogin.teamname#>  is going to check if they are exactly the same, not like.

    <cfelseif  qlogin.teamname contains trim(form.teamname)> may be what you want.

    If you still dont' see why they are not matching try adding this to the cfelse to see what they are really comparing.

       <cfelse>

    "#qlogin.teamname#" vs "#form.teamname#"<br>

    </cfif>