Page 1 of 1

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

Posted: Fri Dec 23, 2011 7:25 am
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 ;)

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

Posted: Fri Dec 23, 2011 2:01 pm
by Temor
can you post the code for set_profile_info?

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

Posted: Fri Dec 23, 2011 3:05 pm
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);
}

?>

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

Posted: Fri Dec 23, 2011 4:32 pm
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 :)

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

Posted: Mon Dec 26, 2011 2:30 am
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.

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

Posted: Mon Dec 26, 2011 2:37 am
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 :)

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

Posted: Mon Dec 26, 2011 5:24 am
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);
}

?>

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

Posted: Mon Dec 26, 2011 8:01 pm
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.

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

Posted: Wed Dec 28, 2011 3:14 am
by SamuelSHP
@temor

:( code that given to me don't change the user information :cry:

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

Posted: Wed Dec 28, 2011 3:51 am
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?

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

Posted: Wed Dec 28, 2011 11:41 pm
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.

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

Posted: Tue May 29, 2012 4:12 pm
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.