PHP edit profile page does not update database
Posted: Sat Dec 24, 2011 9:00 am
I am almost done the edit profile page on my site. I have been following this tutorial: http://www.youtube.com/watch?v=2gcKp6hcSAY
For some reason, when I click on update, the page simply refreshes and nothing is updated in the database. Do you have any idea why? Thanks again for any help.
Relevant files:
core/inc/user.inc.php (rows 46-59)
For some reason, when I click on update, the page simply refreshes and nothing is updated in the database. Do you have any idea why? Thanks again for any help.
Relevant files:
core/inc/user.inc.php (rows 46-59)
<?php $link = mysql_connect('localhost','root','1234') or die('Cannot connect to database'); mysql_select_db('youtube'); //1 function fetch_users(){ $result = mysql_query('SELECT `id`, `username` FROM `user`') or die(mysql_error()); $users = array(); //2 while (($row = mysql_fetch_assoc($result)) !== false){ $users[] = $row; } return $users; } function fetch_user_info($id){ $id = (int)$id; $sql = "SELECT `username`, `email`, `description`, `birthdate`, `firstname`, `lastname`, `gender`, `city`, `country`, `datereg` FROM `user` WHERE `id` = {$id}"; $result = mysql_query($sql); return mysql_fetch_assoc($result); } $username = $_SESSION['username']; $query = mysql_query("SELECT id FROM user WHERE username='$username'") or die(mysql_error()); if (!$query){ echo mysql_error(); } $row = mysql_fetch_row($query); function set_profile_info ($email, $description, $city, $country){ $email = mysql_real_escape_string(htmlentities($email)); $city = mysql_real_escape_string($city); $country = mysql_real_escape_string($country); $description = mysql_real_escape_string(nl2br(htmlentities($description))); $sql = "UPDATE 'users' SET 'email' = '{$email}', 'city' = '{$city}', 'country' = '{$country}', 'description' = '{$description}' WHERE 'id' = {$row[0]}"; mysql_query($sql); } ?>edit_profile.php
<?php session_start(); include('core/inc/user.inc.php'); if (isset($_POST['email'], $_POST['city'], $_POST['country'], $_POST['description'])){ $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['city'], $_POST['country'], $_POST['description']); } $user_info = array( 'email' => htmlentities($_POST['email']), 'descriptiom' => htmlentities($_POST['city']), 'city' => htmlentities($_POST['country']), 'country' => htmlentities($_POST['description']) ); } else{ $user_info = fetch_user_info($row[0]); } ?> <html> <head> <style type="text/css"> form {margin: 10px 0 0 0;} form div {float:left; clear:both; margin: 0 0 4px 0;} label{float:left; width:100px;} input[type="text"], textarea {float:left; width:400px;} input[type="submit"] {margin:10px 0 0 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> <br> <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">City:</label> <input type="text" name="city" id="city" value="<?php echo $user_info['city']; ?>" /> </div> <div> <label for="location">Country:</label> <input type="text" name="country" id="country" value="<?php echo $user_info['country']; ?>" /> </div> <div> <label for="about">About me:</label> <textarea name="about" id="about" rows="14" cols="50"><?php echo strip_tags($user_info['description']); ?></textarea> </div> <div> <input type="submit" value="Update" /> </div> </body> </html>