Page 1 of 1

User Management function(s) problem.

Posted: Wed Sep 10, 2003 12:32 am
by tomasz
In this code, there is a function, show_account(), that populates a registered users account profile. It seems to be working, but there is a problem with the update_account() function, it will update all values in the form except for userid and username. These values are set as session variables in the login script and are then used to show login status at the top of each page.

I've tried using session_destroy() to remove the username and userid values so they can be updated by the update function. Not sure what I'm doing wrong.

This is my first attempt at writing some kind of user registration script. I have everything working except this "My Account" page.

Any help would be appreciated.

Thanks,
Tomasz

Code: Select all

<?php
session_start();
header("Cache-control:private");
include("includes/common-db.php");

function inuse($userid)
   {
   global $user_tablename, $error;
   $query = "SELECT userid FROM user WHERE userid = '$userid'";
   $result = mysql_query($query);
   if(!mysql_num_rows($result)) return 0;
      else return 1;
   }

   function confirmation_mail($useremail, $userid, $username, $password)
   {
       $mail_to = $useremail;
       $mail_subject = "Your JibberJabber Registration";
       $mail_body = "$username, thank you for updating your JibberJabber.com user account\n\n";
       $mail_body .= "Keep this record of your User I.D. and Password.\n\n";
       $mail_body .= "User I.D: $userid\n";
       $mail_body .= "Password: $password.\n\n";
       $mail_body .= "To update your account info, go to http://localhost/beg-php/sample-db-p411/index.php, log in, and view your account.";
       if(mail($mail_to, $mail_subject, $mail_body))
       {
         $confirmation_msg = "A confirmation message has been mailed to $useremail.";
       }
       else
       {
       echo "Failed to send confirmation message. Please <a href='contact.php'>contact</a> JibberJabber.com if you have trouble logging in on your next visit";
       }
}

   function update_account()
   {

      // these globals make the variables available to db_connect
      // and inside the input values for the successful registration message.

      global $userid,$username,$useremail,$password;
      global $address1,$address2,$usercity,$userstate,$usercountry,$userprofile, $error;
      global $user_tablename, $default_dbname, $PHP_SELF;


      $link_id = db_connect('sample_db');

        $query2 = "UPDATE user SET userid = '$userid', username = '$username', useremail = '$useremail',
                   address1 = '$address1', address2 = '$address2', usercity = '$usercity',
                   userstate = '$userstate', usercountry = '$usercountry', userprofile = '$userprofile'
                   where username='$username'";
        $q_res = mysql_query($query2);


        //confirmation_mail($useremail, $userid, $username, $password);

  }

    function show_account()
	    {
	          global $userid,$username,$useremail,$password;
			  global $address1,$address2,$usercity,$userstate,$usercountry,$userprofile, $error, $PHP_SELF;
			  global $default_dbname;


	          $link_id = db_connect('sample_db');
	          $query = "SELECT * from user WHERE userid = '$userid'";
	          $result = mysql_query($query);

	          if (!$result) sql_error();
	          else
	          {
	               $data = mysql_fetch_array($result);

	              // fill the variables with result data

	           $userid = $data['userid'];
	           $username = $data['username'];
	           $address1 = $data['address1'];
	           $address2 = $data['address2'];
	           $usercity = $data['usercity'];
	           $userstate = $data['userstate'];
	           $usercountry = $data['usercountry'];
	           $useremail = $data['useremail'];
	           $userprofile = $data['userprofile'];
	           $password = $data['userpassword'];
	           }
     }
           ?>

   <?php
   function account_form()
   {
   global $userid,$username,$useremail,$password;
   global $address1,$address2,$usercity,$userstate,$usercountry,$userprofile, $error, $PHP_SELF;

   ?>
   <table border="1" cellpadding="0" cellspacing="0" width="100%">
   <tr>
     <td width="20%" height="100" align="left" valign="top" style="background-color:#cccccc">
     &nbsp;
     </td>
     <td width="80%" align="left" valign="top" style="background-color: #cccccc">
     <?php access_status();?>
     <h1 style="color:$ffffff; font-size:28px;background-color:transparent;">JibberJabber.com</h1>
     </td>
   </tr>

   <tr>
     <td height="400" valign="top">
         <p class="leftNav"><a href='jibber.php'>New JibberJabber</a><br>
	         <a href='jabber.php'>Top 10 Jabber</a><br>
	         <a href='reader-jabber.php'>Speak</a><br>
	      <a href='contact.php'>Contact</a><br>
     <a href='index.php'>Home</a></p>
     </td>
     <td valign="top">
     <?php echo $not_logged;?>
     <h2>My JibberJabber Account:</h2>
     <p class="mainContent">You may change your account details by entering new values and clicking
     the 'Update Account' button.</p>
     <p class="mainContent">To change your password, go to the <a href='login.php'>login page.</a></p>

     <!--user account form and table-->
     <div style="margin-left: 10px;">
     <form method="post" action="<?php echo $PHP_SELF;?>">
     <input type="hidden" name="action" value="update">
       <table border="0" width="100%" cellpadding="0" cellspacing="0">
         <tr>
          <td align="left">
          User I.D:
          </td>
          <td>
          <input type="text" name="userid" value="<?php echo $userid;?>" size="24">
          </td>
         </tr>
         <tr>
          <td>
          Real name:
          </td>
          <td>
          <input type="text" name="username" value="<?php echo $username;?>" size="24">
          </td>
        </tr>

        <tr>
         <td align="left">
         E-mail:
         </td>
         <td>
         <input type="text" name="useremail" value="<?php echo $useremail;?>" size="24">
         </td>
        </tr>
        <tr>
         <td align="left">
         Address:
         </td>
         <td align="left">
         <input type="text" name="address1" value="<?php echo $address1;?>" size="24">
         </td>
        </tr>
        <tr>
         <td align="left">
         Address 2 (if you need more space):
         </td>
         <td align="left">
         <input type="text" name="address2" size="24" value="<?php echo $address2;?>">
         </td>
        </tr>
        <tr>
         <td align="left">
         City:
         </td>
         <td align="left">
         <input type="text" name="usercity" size="24" value="<?php echo $usercity;?>">
         </td>
        </tr>

<tr>
        <td align="left">
        Province/State:
        </td>
        <td align="left">
                      <select name="userstate">
                        <option selected><?php echo $userstate;?></option>
                        <option>Alberta</option>
                        <option>British Columbia</option>
                        <option>Saskatchewan</option>
                        <option>Manitoba</option>
                        <option>Ontario</option>
                        <option>Yukon</option>
                        <option>Northwest Territories</option>
                        <option>Nunavut</option>
                        <option>New Brunswick</option>
                        <option>Nova Scotia</option>
                        <option>Newfoundland/Labrador</option>
                        <option>Prince Edward Island</option>
                        <option>Quebec</option>
                        <option>Alabama</option>
                        <option>Alaska</option>
                        <option>Arizona</option>
                        <option>Arkansas</option>
                        <option>California</option>
                        <option>Colorado</option>
                        <option>Connecticut</option>
                        <option>Delaware</option>
                        <option>District of Columbia</option>
                        <option>Florida</option>
                        <option>Georgia</option>
                        <option>Hawaii</option>
                        <option>Idaho</option>
                        <option>Illinois</option>
                        <option>Indiana</option>
                        <option>Iowa</option>
                        <option>Kansas</option>
                        <option>Kentucky</option>
                        <option>Louisiana</option>
                        <option>Maine</option>
                        <option>Maryland</option>
                        <option>Massachusetts</option>
                        <option>Michigan</option>
                        <option>Minnesota</option>
                        <option>Mississippi</option>
                        <option>Missouri</option>
                        <option>Montana</option>
                        <option>Nebraska</option>
                        <option>Nevada</option>
                        <option>New Hampshire</option>
                        <option>New Jersey</option>
                        <option>New Mexico</option>
                        <option>New York</option>
                        <option>North Carolina</option>
                        <option>North Dakota</option>
                        <option>Ohio</option>
                        <option>Oklahoma</option>
                        <option>Oregon</option>
                        <option>Pennsylvania</option>
                        <option>Rhode Island</option>
                        <option>South Carolina</option>
                        <option>South Dakota</option>
                        <option>Tennessee</option>
                        <option>Texas</option>
                        <option>Utah</option>
                        <option>Vermont</option>
                        <option>Virginia</option>
                        <option>Washington</option>
                        <option>West Virginia</option>
                        <option>Wisconsin</option>
                        <option>Wyoming</option>
                        </select>
        </td>
        </tr>

        <tr>
         <td align="left">
         Country:
         </td>
         <td align="left">
         <select name="usercountry">
         <option selected><?php echo $usercountry;?></option>
         <option>Canada</option>
         <option>United States</option>
         </td>
        </tr>        <tr>
         <td align="left">
         Your profile:<br>
         (Say something about yourself)
         </td>
         <td align="left">
         <textarea name="userprofile" cols="30" rows="5"><?php echo $userprofile;?></textarea>
         </td>
        </tr>
         <tr>
   	   <td align="left">
   	   &nbsp;
   	   </td>
   	   <td align="left">
   	   <input type="submit" value="Update Account">
   	   </td>
        </tr>
     </table>
     </form>
     </div>
     </td>
     </tr>
     </table>

    <?php
	  html_footer();
	  } // end account_form


	  /*the variable $action has been set in a hidden input so it is available
	  to perform the switch. When we first enter, the $action variable is empty
	  so the switch default runs( html_header, show_account(),and html_footer).
	  This will display the user account details. After the update form is submitted
	  the action will equal "update" (cuz of the value of the hidden input named 'action')
	  thus the update_function will run.*/

	  switch($action) {
	  case "update":
	  html_header('JibberJabber.com User Registration', 'global.css');
	  update_account();
	  show_account();
	  account_form();
	  break;

	  default:
	  html_header('JibberJabber.com User Registration', 'global.css');
	  show_account();
	  account_form();
	  html_footer();
	  break;
	  }
  ?>

Posted: Wed Sep 10, 2003 10:01 pm
by tsg
I didn't read the whole code, just what you wrote (long day and beer at hand) .. but if I understand your question correctly .. you will want to change the username and userid variables names after you pull it from the DB.

For example, instead of
$userid = $data['userid'];
try calling it

$userid_edit = $data['userid'];

Then also that way through the update database query.

But, if they change their userid and username, then you will need to change it in the session variable.

I hope that helps some ...

User Management problems

Posted: Sat Sep 13, 2003 9:35 am
by tomasz
Thanks, your suggestion worked.

Tomasz