empty query
Posted: Thu Aug 15, 2013 9:59 am
Hi. I was following one of your tutorials on updating user profile info (PHP Tutorial: User Profiles). Yet, I have encountered a problem while trying out the codes. it states that the query was empty. May I know which part of the code did I do wrongly?
userinc.php
[syntax=php]<?php
function fetch_users() {
$result = mysql_query("SELECT `user_id` AS `id`, `user_name` AS `username`, `email` AS `email`, `description` AS `description` FROM `userprofile`");
$users = array();
while (($row = mysql_fetch_assoc($result)) !== false) {
$users[] = $row;
}
return $users;
}
// fetch profile info for the given user
function fetch_user_info($id) {
$id = (int)$id;
$sql = "SELECT `user_id` AS `id`, `user_name` AS `username`, `email` AS `email`, `description` AS `description` FROM `userprofile` WHERE `user_id` = '$id'";
$result = mysql_query($sql);
return mysql_fetch_assoc($result);
}
// update the current users profile info
function set_profile_info($username, $email, $description){
$username = mysql_real_escape_string(htmlentities($username));
$email = mysql_real_escape_string(nl2br(htmlentities($email)));
$description = mysql_real_escape_string(htmlentities($description));
$id = $_GET['id'];
$sql = "UPDATE `userprofile` SET `user_name` = '$username'
`email` = '$email'
`description` = '$description'
WHERE user_id =". '$id' ;
mysql_query($sql);
}
if (!mysql_query($sql)) {
die ('Error: '.mysql_error());
}
?>[/syntax]
editprofile.php
[syntax=php]<?php
include ('connect.php');
if (isset($_POST['username'], $_POST['email'], $_POST['description'])) {
$errors = array();
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
$errors[] = 'Invalid email address!';
}
if (empty($errors)) {
set_profile_info($_POST['username'],$_POST['email'], $_POST['description']);
}
$user_info = array(
'username' => htmlentities($_POST['username']),
'email' => htmlentities($_POST['email']),
'description' => htmlentities($_POST['description'])
) ;
}
else {
$user_info = fetch_user_info($_GET['id']); //change to $_SESSION once the user is logged in, successfully
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<style type = "text/css">
form { margin: 10px 8px 8px 8px; }
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: 18px 8px 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 = "username">Username:</label>
<input type = "text" name = "username" id = "username" value = "<?php echo $user_info['username']; ?>" />
</div>
<div>
<label for = "email">Email:</label>
<input type = "text" name = "email" id = "email" value = "<?php echo $user_info['email']; ?>" />
</div>
<div>
<label for = "description">Description:</label>
<textarea name = "description" id = "description" rows = "14" cols = "50"><?php echo strip_tags($user_info['description']); ?></textarea>
</div>
<div>
<input type = "submit" value = "Update" />
</div>
</form>
</body>
</html>[/syntax]
userinc.php
[syntax=php]<?php
function fetch_users() {
$result = mysql_query("SELECT `user_id` AS `id`, `user_name` AS `username`, `email` AS `email`, `description` AS `description` FROM `userprofile`");
$users = array();
while (($row = mysql_fetch_assoc($result)) !== false) {
$users[] = $row;
}
return $users;
}
// fetch profile info for the given user
function fetch_user_info($id) {
$id = (int)$id;
$sql = "SELECT `user_id` AS `id`, `user_name` AS `username`, `email` AS `email`, `description` AS `description` FROM `userprofile` WHERE `user_id` = '$id'";
$result = mysql_query($sql);
return mysql_fetch_assoc($result);
}
// update the current users profile info
function set_profile_info($username, $email, $description){
$username = mysql_real_escape_string(htmlentities($username));
$email = mysql_real_escape_string(nl2br(htmlentities($email)));
$description = mysql_real_escape_string(htmlentities($description));
$id = $_GET['id'];
$sql = "UPDATE `userprofile` SET `user_name` = '$username'
`email` = '$email'
`description` = '$description'
WHERE user_id =". '$id' ;
mysql_query($sql);
}
if (!mysql_query($sql)) {
die ('Error: '.mysql_error());
}
?>[/syntax]
editprofile.php
[syntax=php]<?php
include ('connect.php');
if (isset($_POST['username'], $_POST['email'], $_POST['description'])) {
$errors = array();
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
$errors[] = 'Invalid email address!';
}
if (empty($errors)) {
set_profile_info($_POST['username'],$_POST['email'], $_POST['description']);
}
$user_info = array(
'username' => htmlentities($_POST['username']),
'email' => htmlentities($_POST['email']),
'description' => htmlentities($_POST['description'])
) ;
}
else {
$user_info = fetch_user_info($_GET['id']); //change to $_SESSION once the user is logged in, successfully
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<style type = "text/css">
form { margin: 10px 8px 8px 8px; }
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: 18px 8px 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 = "username">Username:</label>
<input type = "text" name = "username" id = "username" value = "<?php echo $user_info['username']; ?>" />
</div>
<div>
<label for = "email">Email:</label>
<input type = "text" name = "email" id = "email" value = "<?php echo $user_info['email']; ?>" />
</div>
<div>
<label for = "description">Description:</label>
<textarea name = "description" id = "description" rows = "14" cols = "50"><?php echo strip_tags($user_info['description']); ?></textarea>
</div>
<div>
<input type = "submit" value = "Update" />
</div>
</form>
</body>
</html>[/syntax]