User Profile - edit profile not working.
Posted: Mon Aug 13, 2012 9:38 am
I followed the youtube video on creating a user profile system, and have got the profile part going, but the edit profile page doesn't seem to update my database. I have check the code for errors and can't seem to find anything. When I type into the edit_profile page fields and submit, I get the imploded message, "Your profile has been updated." but nothing has changed in the database.
Please help.
edit_profile.php
[syntax=php]<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/members/core/init.inc.php";
include_once($path);
if (isset($_POST['email'], $_POST['location'], $_POST['info'])){
$errors = array();
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
$errors[] = 'The email is not valid.';
}
if (preg_match('#^[a-z0-9 ]+$#i', $_POST['location']) === 0){
$errors[] = 'Your location must...';
}
if (empty($errors)){
set_profile_info($_POST['email'], $_POST['location'], $_POST['info']);
}
$user_info = array(
'email' => htmlentities($_POST['email']),
'location' => htmlentities($_POST['location']),
'info' => htmlentities($_POST['info'])
);
}else{
$user_info = fetch_user_info($_SESSION['id']);
}
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/members/includes/header.php";
include_once($path);
?>
<html>
<title>Edit Your Profile</title>
<body>
<div>
<?php
if (isset($errors) === false){
echo '';
}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" class="registration_form">
<fieldset>
<legend>Edit Details</legend>
<p>Edit Your Profile <span style="background:#EAEAEA none repeat scroll 0 0;line-height:1;margin-left:210px;;padding:5px 7px;">Already a member? <a href="login.php">Log in</a></span> </p>
<div class="elements">
<label for="email">Email:</label>
<input type="text" name="email" id="email" size="25" value="<?php echo $user_info['email']; ?>"></input>
</div>
<div class="elements">
<label for="location">Location:</label>
<input type="text" name="location" id="location" size="25" value="<?php echo $user_info['location']; ?>"></input>
</div>
<div class="elements">
<label for="info">Info:</label>
<textarea name="info" id="info" rows="8" cols="50"><?php echo $user_info['info']; ?></textarea>
</div>
<div class="submit">
<input type="submit" value="Update" />
</div>
</form>
</body>
</html>
</div>
<br>
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/menu.php";
include_once($path);
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/members/includes/footer.php";
include_once($path);
?> [/syntax]
init.inc.php
[syntax=php]<?php
//Database connection.
session_start();
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = 'admin';
$DB_NAME = 'members';
$conn = mysql_connect($DB_HOST, $DB_USER, $DB_PASS) or die('<h3>Couldn\'t Connect To MySQL Server</h3><h4>Reason:</h4><p>' . mysql_error() . '</p>');
mysql_select_db($DB_NAME, $conn) or die('<h3>Couldn\'t Select MySQL Database [' . $DB_NAME . ']</h3><h4>Reason:</h4><p>' . mysql_error($conn) . '</p>');
include("user.inc.php");
//login in check.
$_SESSION['id'] = 1
?>[/syntax]
user.inc.php
[syntax=php]<?php
//fetches all of the users from the table.
function fetch_users(){
$result = mysql_query("SELECT `id` AS `id`, `company_name` AS `company_name` FROM `trades`");
$users = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$users[] = $row;
}
return $users;
}
//fetches profile info.
function fetch_user_info($id){
$id = (int)$id;
$sql = "SELECT
`id` AS `id`,
`firstname` AS `firstname`,
`surname` AS `surname`,
`company_name` AS `company_name`,
`email` AS `email`,
`location` AS `location`,
`info` AS `info`
FROM `trades`
WHERE `id` = {$id}";
$result = mysql_query($sql);
return mysql_fetch_assoc($result);
}
//updates the current users profile info.
function set_profile_info($email, $location, $info){
$email = mysql_real_escape_string(htmlentities($email));
$location = mysql_real_escape_string($location);
$info = mysql_real_escape_string(nl2br(htmlentities($info)));
$sql = "UPDATE `trades` SET
`email` = `{$email}`
`location` = `{$location}`
`info` = `{$info}`
WHERE `id` = {$_SESSION['id']}";
mysql_query($sql);
}
?>[/syntax]
Please help.
edit_profile.php
[syntax=php]<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/members/core/init.inc.php";
include_once($path);
if (isset($_POST['email'], $_POST['location'], $_POST['info'])){
$errors = array();
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
$errors[] = 'The email is not valid.';
}
if (preg_match('#^[a-z0-9 ]+$#i', $_POST['location']) === 0){
$errors[] = 'Your location must...';
}
if (empty($errors)){
set_profile_info($_POST['email'], $_POST['location'], $_POST['info']);
}
$user_info = array(
'email' => htmlentities($_POST['email']),
'location' => htmlentities($_POST['location']),
'info' => htmlentities($_POST['info'])
);
}else{
$user_info = fetch_user_info($_SESSION['id']);
}
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/members/includes/header.php";
include_once($path);
?>
<html>
<title>Edit Your Profile</title>
<body>
<div>
<?php
if (isset($errors) === false){
echo '';
}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" class="registration_form">
<fieldset>
<legend>Edit Details</legend>
<p>Edit Your Profile <span style="background:#EAEAEA none repeat scroll 0 0;line-height:1;margin-left:210px;;padding:5px 7px;">Already a member? <a href="login.php">Log in</a></span> </p>
<div class="elements">
<label for="email">Email:</label>
<input type="text" name="email" id="email" size="25" value="<?php echo $user_info['email']; ?>"></input>
</div>
<div class="elements">
<label for="location">Location:</label>
<input type="text" name="location" id="location" size="25" value="<?php echo $user_info['location']; ?>"></input>
</div>
<div class="elements">
<label for="info">Info:</label>
<textarea name="info" id="info" rows="8" cols="50"><?php echo $user_info['info']; ?></textarea>
</div>
<div class="submit">
<input type="submit" value="Update" />
</div>
</form>
</body>
</html>
</div>
<br>
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/menu.php";
include_once($path);
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/members/includes/footer.php";
include_once($path);
?> [/syntax]
init.inc.php
[syntax=php]<?php
//Database connection.
session_start();
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = 'admin';
$DB_NAME = 'members';
$conn = mysql_connect($DB_HOST, $DB_USER, $DB_PASS) or die('<h3>Couldn\'t Connect To MySQL Server</h3><h4>Reason:</h4><p>' . mysql_error() . '</p>');
mysql_select_db($DB_NAME, $conn) or die('<h3>Couldn\'t Select MySQL Database [' . $DB_NAME . ']</h3><h4>Reason:</h4><p>' . mysql_error($conn) . '</p>');
include("user.inc.php");
//login in check.
$_SESSION['id'] = 1
?>[/syntax]
user.inc.php
[syntax=php]<?php
//fetches all of the users from the table.
function fetch_users(){
$result = mysql_query("SELECT `id` AS `id`, `company_name` AS `company_name` FROM `trades`");
$users = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$users[] = $row;
}
return $users;
}
//fetches profile info.
function fetch_user_info($id){
$id = (int)$id;
$sql = "SELECT
`id` AS `id`,
`firstname` AS `firstname`,
`surname` AS `surname`,
`company_name` AS `company_name`,
`email` AS `email`,
`location` AS `location`,
`info` AS `info`
FROM `trades`
WHERE `id` = {$id}";
$result = mysql_query($sql);
return mysql_fetch_assoc($result);
}
//updates the current users profile info.
function set_profile_info($email, $location, $info){
$email = mysql_real_escape_string(htmlentities($email));
$location = mysql_real_escape_string($location);
$info = mysql_real_escape_string(nl2br(htmlentities($info)));
$sql = "UPDATE `trades` SET
`email` = `{$email}`
`location` = `{$location}`
`info` = `{$info}`
WHERE `id` = {$_SESSION['id']}";
mysql_query($sql);
}
?>[/syntax]