Update database field - user must log off and log back in for changes to take effect

Discussion in 'Programming' started by lespaul00, Jan 20, 2008.

  1. #1
    I have the following on one of my pages called display.cfm:

    
    <cfquery name="data" datasource="mydatabase">
    SELECT USER_FIRST_NAME
    FROM TBLUSER
    WHERE USER_ID = #SESSION.auth.USER_ID#
    </cfquery>
    
    <cfoutput>
    #data.USER_FIRST_NAME#
    </cfoutput>
    
    <a href="edit.cfm">Edit</a>
    
    
    Code (markup):
    This simply displays the user's name. Upon clicking "edit" it takes them to the edit.cfm page with the following code:


    <cfquery name="data" datasource="mydatabase">
    SELECT USER_FIRST_NAME
    FROM TBLUSER
    WHERE USER_ID = #SESSION.auth.USER_ID#
    </cfquery>
    
    <cfoutput>
    <form method="post" action="edit_submit.cfm?page="#CGI.SCRIPT_NAME#">
    <input type="text" value"#data.USER_FIRST_NAME#" name="USER_FIRST_NAME">
    <input type="submit" value="change!">
    </cfoutput>
    Code (markup):
    This page shows the same thing, except, the First name is shown in a text box. The user can change his/her first name, then click "submit" to go to the submit page. Here is the submit page:

    
    <cfquery name="change" datasource="mydatabase">
    UPDATE TBLUSER
    SET USER_FIRST_NAME = form.USER_FIRST_NAME
    WHERE USER_ID = #SESSION.auth.USER_ID#
    </cfquery>
    
    <cfoutput>
    <cflocation url="#URL.PAGE#">
    </cfoutput>
    
    
    Code (markup):
    So, once changed, it will bring the user back to the edit page. My problem is, the new USER_FIRST_NAME value updates in the database, but not on this page the user is redirected to. Even refreshing the page doesn't update the field. The user MUST log off then log back in for the update to be shown! Why is this? What can I do to prevent the user to have to log out then back in?

    Thanks!
     
    lespaul00, Jan 20, 2008 IP
  2. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #2
    First try these small changes in red,

    The single quotes delimit the query string, not sure what that means but it seems to be important.,

    In the form you had an extra double qoute around the #cgi.script_name# that you didnt need.
    
    <cfquery name="data" datasource="mydatabase">
    SELECT USER_FIRST_NAME
    FROM TBLUSER
    WHERE USER_ID = [COLOR="Red"]'[/COLOR]#SESSION.auth.USER_ID#[COLOR="red"]'[/COLOR]
    </cfquery>
    
    <cfoutput>
    <form method="post" action="[COLOR="red"]edit_submit.cfm?page=#CGI.SCRIPT_NAME#[/COLOR]">
    <input type="text" value"#data.USER_FIRST_NAME#" name="USER_FIRST_NAME">
    <input type="submit" value="change!">
    </cfoutput>
    
    Code (markup):

    And this on the edit_submit.cfm

    
    <cfquery name="change" datasource="mydatabase">
    UPDATE TBLUSER
    SET USER_FIRST_NAME = [COLOR="red"]'[/COLOR]#form.USER_FIRST_NAME#[COLOR="red"]'[/COLOR]
    WHERE USER_ID = [COLOR="red"]'[/COLOR]#SESSION.auth.USER_ID#[COLOR="red"]'[/COLOR]
    </cfquery>
    
    Code (markup):
    I dont think this is your problem but it doesnt hurt to start with the obvious stuff first.
     
    unitedlocalbands, Jan 21, 2008 IP
  3. lespaul00

    lespaul00 Peon

    Messages:
    283
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think I know what I did...

    What I wrote was an example. The variables I am trying to edit are session variables. So obviously, they will only update once the session is terminated and the user logs back in.

    On my submit page, I will structdelete the edited session variables, and instantaneously re-create them. This should work.

    Regardless, thanks for the help!
     
    lespaul00, Jan 21, 2008 IP
  4. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #4
    I thought that might be your problem when I first read your post but after looking closer I saw that you wern't using any session variable.

    I dont think you have to delete the session variable, just update the database like in your example and then set the session variable to the new user_first_name in the database.



    
    <cfquery name="change" datasource="mydatabase">
    UPDATE TBLUSER
    SET USER_FIRST_NAME = '#form.USER_FIRST_NAME#'
    WHERE USER_ID = '#SESSION.auth.USER_ID#'
    </cfquery>
    
    <cfquery name="data" datasource="mydatabase">
    SELECT USER_FIRST_NAME
    FROM TBLUSER
    WHERE USER_ID = '#SESSION.auth.USER_ID#'
    </cfquery>
    
    <cfset session.user_first_name = '#data.user_first.name#'>
    
    <cfoutput>
    <cflocation url="#URL.PAGE#">
    </cfoutput>
    
    Code (markup):
     
    unitedlocalbands, Jan 21, 2008 IP