Problem On Profile Page -- Can't edit the user information

Post here is you are having problems with any of the tutorials.
Post Reply
User avatar
SamuelSHP
Posts: 6
Joined: Fri Dec 23, 2011 6:47 am

Problem On Profile Page -- Can't edit the user information

Post by SamuelSHP »

Hi, i watched BetterPHP's video on Youtube about User Profile. I think it's a very good tutorial. I type all of the PHP code. At last, i want to edit the user information on edit_profile.php but i get a problem. The user information in profile.php does not change.

This is my edit_profile.php code
<?php

include('core/init.inc.php');

if (isset($_POST['email'], $_POST['location'], $_POST['about'])) {
    $errors = array();

    if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
        $errors[] = 'The email address you entered is not valid';
    }

    if (preg_match('#^[a-z0-9]+$#i', $_POST['location']) === 0){
       $errors[] = 'Your location must only contain a-z, 0-9 and spaces.';
    }

    if (empty($errors)){
        set_profile_info($_POST['email'], $_POST['about'], $_POST['location']);
    }

$user_info = array(
  'email'       => htmlentities($_POST['email']),
  'about'       => htmlentities($_POST['about']),
  'location'    => htmlentities($_POST['location']),
);
}else{
    $user_info =fetch_user_info($_SESSION['uid']);
}

?>
 <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html"; charset=utf-8"/>
        <style type="text/css">
            form { margin :10px 0px 0px 0px; }
            form div {float:left; clear:both; margin:0px 0px 4px 0px;}
            label { float: left; width: 100px;}
            input[type="text"], textarea{float: left; width:400px; }
            input[type="submit"] { margin:10px 0px 0px 100px;}
        </style>
              <title> Edit Your Profile</title>
    </head>
    <body>
        <div>
            <?php

if (isset($errors) === false){
    echo 'Click Update to edit your profile';
}else if (empty($errors)){
    echo 'Your profile has been updated.';
}else{
    echo '<ul><li>', implode('</li><li>', $errors),'</li> </ul>';
    }
            
            ?>
        </div>
        <form action="" method="post">
            <div>
                <label for="email">Email:</label>
                <input type="text" name="email" id="email" value="<?php echo $user_info['email']; ?>"/>
            </div>
            <div>
                <label for="location">Location:</label>
                <input type="text" name="location" id="location" value="<?php echo $user_info['location']; ?>"/>
            </div>
            <div>
                <label for="about">About Me:</label>
                <textarea name="about" id="about" rows="14" cols="50"/><?php  echo strip_tags($user_info['about']); ?></textarea>
            </div>
            <div>
                <input type="submit" value="Update" />
            </div>
        </form>
    </body>
</html>
The profile.php code
<?php

include('core/init.inc.php');

$user_info = fetch_user_info($_GET['uid']);

?>
<html>
    <head>
        <title><?php echo $user_info['username']; ?>'s Profile</title>
    </head>
    <body>
        <div>
            <?php

            if ($user_info === false){
                echo 'That user does not exist.';
            }else{ 
            ?>
            <h1><?php echo $user_info['firstname']; ?> <?php echo $user_info['lastname']; ?></h1>
            <p>Username: <?php echo $user_info['username']; ?></p>
            <p>Gender: <?php echo ($user_info['gender']== 1) ? 'Male' : 'Female'; ?></p>
            <p>Email: <?php echo $user_info['email']; ?></p>
            <p>Location: <?php echo $user_info['location']; ?></p>
            <p><?php echo $user_info['about']; ?></p>
            <?php
            }

            ?>
        </div>
    </body>
</html>
Sorry for my bad english; Asian People ;)
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Problem On Profile Page -- Can't edit the user informati

Post by Temor »

can you post the code for set_profile_info?
User avatar
SamuelSHP
Posts: 6
Joined: Fri Dec 23, 2011 6:47 am

Re: Problem On Profile Page -- Can't edit the user informati

Post by SamuelSHP »

@ Temor : oh sure..

Here the code :)
<?php

// fetches all of the users from table
function fetch_users(){
    $result = mysql_query('SELECT `user_id` AS `id`, `user_username` AS `username` FROM `users` ' );

    $users = array();

    while (($row = mysql_fetch_assoc($result)) !== false){
        $users[] =$row;
    }

    return $users;
}

// fetches profile information
function fetch_user_info($uid){
    $uid = (int)$uid;

    $sql = "SELECT
        `user_username` AS `username`,
        `user_firstname` AS `firstname`,
        `user_lastname` AS `lastname`,
        `user_email` AS `email`,
        `user_about` AS `about`,
        `user_location` AS `location`,
        `user_gender` AS `gender`
        FROM `users`
        WHERE `user_id` = {$uid}";

        $result = mysql_query($sql);

        return mysql_fetch_assoc($result);
}

// update the current user profile information.
function set_profile_info($email, $about, $location){
    $email      = mysql_real_escape_string(htmlentities($email));
    $about      = mysql_real_escape_string(nl2br(htmlentities($about)));
    $location   = mysql_real_escape_string($location);

    $sql = "UPDATE 'users' SET
        'user_email' = '{$email}',
        'user_about' = '{$about}',
        'user_location' = '{$location}',
        WHERE 'user_id' = {$_SESSION['uid']}";

    mysql_query($sql);
}

?>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Problem On Profile Page -- Can't edit the user informati

Post by jacek »

You need to use backticks ` around your table and columns names instead of quotes '

You did it right in every other function just not in this one :)
Image
User avatar
SamuelSHP
Posts: 6
Joined: Fri Dec 23, 2011 6:47 am

Re: Problem On Profile Page -- Can't edit the user informati

Post by SamuelSHP »

Thanks for the solution, but when i want to try the solution, my XAMPP program get an error, so now i can't try it. :cry:

And if my XAMPP can work normally, next time i will try this solution.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Problem On Profile Page -- Can't edit the user informati

Post by Temor »

SamuelSHP wrote:Thanks for the solution, but when i want to try the solution, my XAMPP program get an error, so now i can't try it. :cry:

And if my XAMPP can work normally, next time i will try this solution.
What kind of error? We might be able to help :)
User avatar
SamuelSHP
Posts: 6
Joined: Fri Dec 23, 2011 6:47 am

Re: Problem On Profile Page -- Can't edit the user informati

Post by SamuelSHP »

My XAMPP know can work normally :mrgreen: Yesterday, Apache on my XAMPP can't running. And i get the solution: "To Disable Windows 7 feature (Internet Information Services) " :)

The solution that given by Jacek, i have try it. But it not change the user profile :( So, i give you all of my "new" code, and if there is a false code, please report back to me :)

Edit Profile.php
<?php

include('core/init.inc.php');

if (isset($_POST['email'], $_POST['location'], $_POST['about'])) {
    $errors = array();

    if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
        $errors[] = 'The email address you entered is not valid';
    }

    if (preg_match('#^[a-z0-9]+$#i', $_POST['location']) === 0){
       $errors[] = 'Your location must only contain a-z, 0-9 and spaces.';
    }

    if (empty($errors)){
        set_profile_info($_POST['email'], $_POST['about'], $_POST['location']);
    }

$user_info = array(
  'email'       => htmlentities($_POST['email']),
  'about'       => htmlentities($_POST['about']),
  'location'    => htmlentities($_POST['location']),
);
}else{
    $user_info =fetch_user_info($_SESSION['uid']);
}

?>
 <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html"; charset=utf-8"/>
        <style type="text/css">
            form { margin :10px 0px 0px 0px; }
            form div {float:left; clear:both; margin:0px 0px 4px 0px;}
            label { float: left; width: 100px;}
            input[type="text"], textarea{float: left; width:400px; }
            input[type="submit"] { margin:10px 0px 0px 100px;}
        </style>
              <title> Edit Your Profile</title>
    </head>
    <body>
        <div>
            <?php

if (isset($errors) === false){
    echo 'Click Update to edit your profile';
}else if (empty($errors)){
    echo 'Your profile has been updated.';
}else{
    echo '<ul><li>', implode('</li><li>', $errors),'</li> </ul>';
    }
            
            ?>
        </div>
        <form action="" method="post">
            <div>
                <label for="email">Email:</label>
                <input type="text" name="email" id="email" value="<?php echo $user_info['email']; ?>"/>
            </div>
            <div>
                <label for="location">Location:</label>
                <input type="text" name="location" id="location" value="<?php echo $user_info['location']; ?>"/>
            </div>
            <div>
                <label for="about">About Me:</label>
                <textarea name="about" id="about" rows="14" cols="50"/><?php  echo strip_tags($user_info['about']); ?></textarea>
            </div>
            <div>
                <input type="submit" value="Update" />
            </div>
        </form>
    </body>
</html>
profile.php
<?php

include('core/init.inc.php');


$user_info = fetch_user_info($_GET['uid']);

?>
<html>
    <head>
        <title><?php echo $user_info['username']; ?>'s Profile</title>
    </head>
    <body>
        <div>
            <?php

            if ($user_info === false){
                echo 'That user does not exist.';
            }else{ 
            ?>
            <h1><?php echo $user_info['firstname']; ?> <?php echo $user_info['lastname']; ?></h1>
            <p>Username: <?php echo $user_info['username']; ?></p>
            <p>Gender: <?php echo ($user_info['gender']== 1) ? 'Male' : 'Female'; ?></p>
            <p>Email: <?php echo $user_info['email']; ?></p>
            <p>Location: <?php echo $user_info['location']; ?></p>
            <p><?php echo $user_info['about']; ?></p>
            <?php
            }

            ?>
        </div>
    </body>
</html>
user_list.php
<?php

include('core/init.inc.php');

?>

<html>
    <head>
        <title>
            Registered Users
        </title>
    </head>
    <body>
        <div>
            <?php

        foreach (fetch_users() as $user){
         ?>
            <p>
<a href ="profile.php?uid=<?php echo $user['id'];?>"><?php echo $user['username'];?></a>
            </p>
            <?php
        }
        
            ?>
        </div>
    </body>
</html>
init.inc.php
<?php

session_start();

mysql_connect('localhost', 'root', '');
mysql_select_db('user_profile');

$path = dirname(__FILE__);

include("{$path}/inc/user.inc.php");

$_SESSION['uid'] = 1;

?>
user.inc.php
<?php

// fetches all of the users from table
function fetch_users(){
    $result = mysql_query('SELECT `user_id` AS `id`, `user_username` AS `username` FROM `users` ' );

    $users = array();

    while (($row = mysql_fetch_assoc($result)) !== false){
        $users[] =$row;
    }

    return $users;
}

// fetches profile information
function fetch_user_info($uid){
    $uid = (int)$uid;

    $sql = "SELECT
        `user_username` AS `username`,
        `user_firstname` AS `firstname`,
        `user_lastname` AS `lastname`,
        `user_email` AS `email`,
        `user_about` AS `about`,
        `user_location` AS `location`,
        `user_gender` AS `gender`
        FROM `users`
        WHERE `user_id` = {$uid}";

        $result = mysql_query($sql);

        return mysql_fetch_assoc($result);
}

// update the current user profile information.
function set_profile_info($email, $about, $location){
    $email      = mysql_real_escape_string(htmlentities($email));
    $about      = mysql_real_escape_string(nl2br(htmlentities($about)));
    $location   = mysql_real_escape_string($location);

    $sql = "UPDATE 'users' SET
        `user_email` = '{$email}',
        `user_about` = '{$about}',
        `user_location` = '{$location}',
        WHERE `user_id` = {$_SESSION['uid']}";

    mysql_query($sql);
}

?>
Last edited by jacek on Mon Dec 26, 2011 2:48 pm, edited 1 time in total.
Reason: removed mediafire link.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Problem On Profile Page -- Can't edit the user informati

Post by Temor »

Try this in your update query:
 $sql = "UPDATE `users` SET
       `user_email` = {$email},
       `user_about` = {$about},
       `user_location` = {$location},
       WHERE `user_id` = {$_SESSION['uid']};
I changed the semiquotes around users to backticks and also removed the semiquotes around the variables.
User avatar
SamuelSHP
Posts: 6
Joined: Fri Dec 23, 2011 6:47 am

Re: Problem On Profile Page -- Can't edit the user informati

Post by SamuelSHP »

@temor

:( code that given to me don't change the user information :cry:
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Problem On Profile Page -- Can't edit the user informati

Post by Temor »

Hmm... I can't spot any typos that could stop the query from running.
Do you get any errors or will it just not update?

What does your database look like?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Problem On Profile Page -- Can't edit the user informati

Post by jacek »

there should not be a , after the final column and you need ' around your strings so
 $sql = "UPDATE `users` SET
      `user_email` = {$email},
      `user_about` = {$about},
      `user_location` = {$location},
      WHERE `user_id` = {$_SESSION['uid']};
should be
 $sql = "UPDATE `users` SET
      `user_email` = '{$email}',
      `user_about` = '{$about}',
      `user_location` = '{$location}'
      WHERE `user_id` = {$_SESSION['uid']};
If you add
echo mysql_error();
after the mysql_query() it should tell you what is wrong.
Image
npor84
Posts: 1
Joined: Tue May 29, 2012 3:40 pm

Re: Problem On Profile Page -- Can't edit the user informati

Post by npor84 »

Hi, i've followed this tutorial, but i can't update my database through edit profile page. I'm using my own login and registration system.
When i log in my website, i click in one link (update profile), then i proceed some changes, then click in 'update' and nothing is changed in the database.
Can you help how to succeed?
Thanks in advance.
Post Reply