Update multiple rows with <cfloop>

Discussion in 'Programming' started by unitedlocalbands, Aug 24, 2007.

  1. #1
    Here is the SQL statment and form. When i run this program I dont get an error nor does it update the rows i would like updated.

    Basicaly I want the user to be able to set the picture they want as their "default" picture for their profile.

    The idea being that the field "DEFAULTPIC" in the database will be set to "1" on the pic they want to be their defaultpic. And the rest will be set to "0"

    I thought using radio buttons was the easy way to do this but I cant even get an error.

    
    
    <cfloop index="defaultpic" list="#form.defaultpic#">
      <cfquery datasource="unitedlocalbands">
        
        UPDATE USER_PICTURES
        SET DEFAULTPIC = #FORM.defaultpic#
        WHERE PICTUREID='#FORM.pictureid#'
    	
      </cfquery>
    </cfloop>
    
    
    <form method="POST" action="<cfoutput>#CurrentPage#</cfoutput>?groupname=<cfoutput>#GROUPNAME#</cfoutput>" name="defaultpic">
    	
    <table cellpadding="6" width="850" align="left" class="login">
      <cfoutput query="browse">
       <cfif browse.currentrow mod 4 eq 1>
     <tr>
       </cfif>
     
        <td align="center" valign="top">
           <input type="hidden" name="pictureid" value="#browse.pictureid#">
    
           Set as Logo:<input type="radio" name="defaultpic" value="0">
    
          <br>
    	 
          <a href="picture_edit_delete.cfm?    rowid=#browse.pictureid#&amp;title=Pictures&amp;client.user_loginid=#client.user_loginid#">Edit / Delete</a><br>
    	 
    <img border="0" width="110"  src="#pictureurl#">
    <br>#caption#<br />
    	 
         </td>
      
       <cfif browse.currentrow mod 4 eq 0>
    </tr>
       </cfif>
     </cfoutput>
      </CFIF> 
    </table>
    <br>
    <br>
    <input type="hidden" name="loginid" value="#client.user_loginid#">
    <input type="hidden" name="MM_UpdateRecord" value="defaultpic">
    <input type="submit" name="Save" value="Save">
    
    </form>	
    
    
    Code (markup):

    I'm using a repeat region to dynamicaly create the radio button group.
    Not sure if thats my problem or not.

    Thanks a bunch for your help.
     
    unitedlocalbands, Aug 24, 2007 IP
  2. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Tip: If you're developing locally, turn on all debugging in the CF Administrator. It will show everything from the form variables to the SQL statements sent to the database. It can be very helpful in diagnosing these kinds of problems :)

    I see a few problems. But most importantly I think you may be misunderstanding how radio buttons and hidden fields work.

    Radio buttons with the same name allow only one of those buttons to be checked at a time. Only the value of the checked button is passed to the action page. Hidden fields with the same name pass all of the values to the action page as a comma delimited list. CFDUMP the values on the action page and you'll see something like this

    
    form.DEFAULTPIC = 0
    form.PICTUREID= pictureId1, pictureId2, pictureId3, ...
    
    Code (markup):
    So the resulting update statement will look like this - which obviously won't work.

    
       UPDATE USER_PICTURES
       SET   DEFAULTPIC = 0
       WHERE PICTUREID='pictureid1,pictureid2,pictureid5,picture8,...'
       
    Code (markup):
    There are a few ways you could set the default picture. But whichever method you choose, don't forget to clear the previous default picture or you'll end up with two records where DEFAULTPIC = 1.
     
    cfStarlight, Aug 25, 2007 IP
  3. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #3
    This may not be the best way to do it and I probably wrote more code than needed.

    But I created a database table to store the picture to be set as the default.
    After the user set their default the first time then the SQL statment changes from "insert" to "update" with a real simple

    
    <cfif default.recordcount eq 1>
    
    <a href="defaultpic.cfm?action=update">Update you Pic</a>
    
    <cfeles>
    
    <a href="default.cfm?action=insert">Set your Pic</a>
    
    </cfif>
    
    Code (markup):
    Thanks for the help.

    If any one has any question please dont hesitate to ask. I will be more than happy to share.
     
    unitedlocalbands, Aug 26, 2007 IP
  4. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You could store the default picture information in a separate table. Though in this case it doesn't seem necessary. A simple boolean column on the main image table "IsDefaultImage" would do just as well IMO.
     
    cfStarlight, Aug 26, 2007 IP