473,467 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ


Profiles Script

By Blair Ireland
Senior Editor, TheScripts.com

Another topic we should be interested in is editing your profile, discussed below.

Here is the HTML you will need for the first part.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE> Log in </TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<FORM METHOD=POST ACTION="http://www.yoursite.com/edit.php3">
<TABLE>
<TR>
    <TD><FONT FACE="ARIAL" SIZE="-1">User Name</FONT></TD>
    <TD><INPUT TYPE="text" NAME="Name"></TD>
</TR>
<TR>
    <TD><FONT FACE="ARIAL" SIZE="-1">Password</FONT></TD>
    <TD><INPUT TYPE="password" NAME="Password"></TD>
</TR>
<TR>
    <TD COLSPAN=2><CENTER><INPUT TYPE="submit" VALUE="Submit"></CENTER></TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>

Again we are pointing the form towards a new file, which will be called edit.php3.

<?php
function showheader ($title) {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE> <?php echo $title ?> </TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<CENTER>
<?php
}

function showfooter () {
?>
</CENTER>
</BODY>
</HTML>
<?php
}

mysql_connect("localhost", "username", "password");
mysql_select_db("users");

Like in register.php3, we make our header/footer functions, and connect to the database.

if ($action == "edit_two") {

Here is something different. This is one of our flow controls for the application. You will notice below that when you submit the form, you are sending to application a hidden value named action with a value of "edit_two". If that part has been sent already, this part of the application is executed. We will look at the else condition later.

    $user = mysql_query("UPDATE personalize SET Password='$Password',News1='$News1',News2='$News2',Weather='$Weather' WHERE (Name='$Name')");

This query is all on one line, but not now as the word wrap gets in the way. Anyways, it updates all the values with their new ones where the Name is equal to the variable $Name, which will be sent as a hidden variable soon.

    setcookie("site_user", $Name, time() + 31536000, "/");
    setcookie("site_pass", $Password, time() + 31536000, "/");

We update the cookies for the user, just really for convenience.

    showheader("Edit Success!");
?>
    You Have Edited Your Profile.<BR>
    User Name: <?php echo $Name ?>
    <BR>
    Password: <?php echo $Password ?>
<?php
    showfooter();
}

else {

Here is our else condition, which is what happens after you hit submit for the first time from the HTML form. The above code is executed after this part of the program.

$query = mysql_query("SELECT * FROM personalize WHERE(Name='$Name' AND Password='$Password')");

The query to grab the information about the user trying to log in is executed.

    if (mysql_num_rows($query) != 1) {
        showheader("That User/Pass Combination was Incorrect");
        echo "The $Name / $Password Combination is incorrect<P>Please Try Again.";
        showfooter();
        exit;
    }

If the user/password combination is not found in the records, we halt the program telling them their combination did not work.

    else {
        $info = mysql_fetch_array($query);
        showheader("Edit ".$info['Name']."'s Profile");

Otherwise, we fetch the info into the array $info, and show our header with the User Name in the title bar.

?>
<FORM METHOD=POST ACTION="<?php echo $PHP_SELF ?>">

What's this? $PHP_SELF's value is that of the name of the file it is in. It is just making sure the form is posted to the correct file, itself.

<INPUT TYPE="hidden" NAME="Name" VALUE="<?php echo $info['Name'] ?>">
<INPUT TYPE="hidden" NAME="action" VALUE="edit_two">

Our hidden form fields. Name is for when we want to update the profile in the next set of code, so we can identify the profile we are updating. The hidden field action is for the flow-control of the program.

<TABLE>
<TR>
    <TD COLSPAN=2><CENTER>Please Select Your Registration Options</CENTER></TD>
</TR>
<TR>
    <TD>Your UserName</TD>
    <TD><?php echo $info['Name'] ?></TD>
</TR>

We can't allow our users to update their usernames. This would cause quite a few possible problems, and to get rid of them we would need a lot of error checking. This just saves us from that nonsense.

<TR>
    <TD>Choose Your Password</TD>
    <TD><INPUT TYPE="password" NAME="Password" size="10" VALUE="<?php echo $info['Password'] ?>"></TD>
</TR>

The value of the current password is put into the password field.

<TR>
    <TD>News Source 1</TD>
    <TD><SELECT NAME="News1">
            <OPTION VALUE="32bitsonline.php3"<?php if ($info['News1'] == "32bitsonline.php3") { echo " SELECTED"; } ?>>32bitsonline</OPTION>
            <OPTION VALUE="freshmeat.php3"<?php if ($info['News1'] == "freshmeat.php3") { echo " SELECTED"; } ?>>Fresh Meat</OPTION>
            <OPTION VALUE="slashdot.php3"<?php if ($info['News1'] == "slashdot.php3") { echo " SELECTED"; } ?>>Slashdot</OPTION>
            <OPTION VALUE="hotwired.php3"<?php if ($info['News1'] == "hotwired.php3") { echo " SELECTED"; } ?>>WebMonkey</OPTION>
            </SELECT>
    </TD>
</TR>

Yuck. I will only have to explain this bit once though, so it's good enough. The next two select boxes follow the same format. Anyways, this is actually a bunch of simple if statements being evaluated so they know what option to mark as "SELECTED". This is so the appropriate option is selected when the user is going to edit their profile.

<TR>
    <TD>News Source 2</TD>
    <TD><SELECT NAME="News2">
            <OPTION VALUE="32bitsonline.php3"<?php if ($info['News2'] == "32bitsonline.php3") { echo " SELECTED"; } ?>>32bitsonline</OPTION>
            <OPTION VALUE="freshmeat.php3"<?php if ($info['News2'] == "freshmeat.php3") { echo " SELECTED"; } ?>>Fresh Meat</OPTION>
            <OPTION VALUE="slashdot.php3"<?php if ($info['News2'] == "slashdot.php3") { echo " SELECTED"; } ?>>Slashdot</OPTION>
            <OPTION VALUE="hotwired.php3"<?php if ($info['News2'] == "hotwired.php3") { echo " SELECTED"; } ?>>WebMonkey</OPTION>            
            </SELECT>
    </TD>
</TR>
<TR>
    <TD>Weather Source</TD>
    <TD><SELECT NAME="Weather">
            <OPTION VALUE="USNY0996"<?php if ($info['Weather'] == "USNY0996") { echo " SELECTED"; } ?>>New York</OPTION>
            <OPTION VALUE="USCA0987"<?php if ($info['Weather'] == "USCA0987") { echo " SELECTED"; } ?>>San Francisco</OPTION>
            <OPTION VALUE="CAXX0504"<?php if ($info['Weather'] == "CAXX0504") { echo " SELECTED"; } ?>>Toronto</OPTION>
            </SELECT>
    </TD>
</TR>
<TR><TD COLSPAN=2><CENTER><INPUT TYPE="submit" Value="Edit Profile!"></CENTER></TD></TR>
</TABLE>
</FORM>
<?php
    showfooter();
    }
}
?>

And We are done!

« Member Script Using The Script »

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.