empty query

Ask about a PHP problem here.
Post Reply
quanyii
Posts: 3
Joined: Thu Aug 15, 2013 9:49 am

empty query

Post by quanyii »

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
<?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());
}
?>
editprofile.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>
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: empty query

Post by Temor »

Could you post the exact error message? it usually contains a hint as to where you should look.

I have a few suggestions on things you should change with your code. I'll get to that once we've solved your initial problem.
quanyii
Posts: 3
Joined: Thu Aug 15, 2013 9:49 am

Re: empty query

Post by quanyii »

Hi, I have managed to solve the problem. However, another problem is that I couldn't update the user's profile image(After watching another PHP tutorial from betterphp on YT).

I was prompted this error after trying to update the user profile image.
Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\1103242B\updateprofile\editprofile.php on line 13
Though the newly updated image could not be displayed and an error message was prompted. Yet, the file(user_avatars) is able to capture the newly updated image and named it after the 'id' of the user. For instance, 1.jpg. I'm not sure which part went wrong.

This is my new code.

userinc.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);
	
	$info = mysql_fetch_assoc($result);
	
	$info['image'] = (file_exists("{$GLOBALS['path']}user_avatars/{$info['id']}.jpg")) ? "user_avatars/{$info['id']}.jpg" : "user_avatars/avatardefault.png";
	
	return $info;
}

// update the current users profile info
function set_profile_info($username, $email, $description, $image){
	$username = mysql_real_escape_string(htmlentities($username));
	$email = mysql_real_escape_string(nl2br(htmlentities($email)));
	$description = mysql_real_escape_string(htmlentities($description));
	
	if(file_exists($image)) {
		$src_size = getimagesize($image);
		
		if($src_size['mime'] == 'image/jpeg') {
			$src_img = imagecreatefromjpeg($image);
		}
		else if($src_size['mime'] == 'image/png') {
			$src_img = imagecreatefrompng($image);
		}
		else if($src_size['mime'] == 'image/gif') {
			$src_img = imagecreatefromgif($image);
		}else {
			$src_img = false;
		}
		
		if ($src_img != false) {
			$thumb_width = 200;
			
			// if the image is smaller than the thumb width set at 200
			if($src_size[0] <= $thumb_width) {
				$thumb = $src_img;
			}
			else { //resize the image
				$new_size[0] = $thumb_width;
				$new_size[1] = ($src_size[1]/$src_size[0]) * $thumb_width; //height/width
				
				$thumb = imagecreatetruecolor($new_size[0], $new_size[1]);
				imagecopyresampled($thumb, $src_img, 0, 0, 0, 0, $new_size[0], $new_size[1], $src_size[0], $src_size[1]);
			}
			
			imagejpeg($thumb, "{$GLOBALS['path']}/user_avatars/{$_GET['id']}.jpg");
		}
	}
	
	$sql = "UPDATE `userprofile` SET `user_name` = '{$username}',
									 `email` = '{$email}',
									 `description` = '{$description}'
								WHERE `user_id` = {$_GET['id']}";
	
	mysql_query($sql);
	
if (!mysql_query($sql)) {
	die ('Error: '.mysql_error());
}

}
?>
editprofile.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($_FILES['image']['tmp_name']) === false) {
		$file_ext = end(explode('.', $_FILES['image']['name']));
		
		if(in_array(strtolower($file_ext), array('jpg','jpeg','png','gif')) === false) {
			$errors[] = 'Your file must be an image.';
		}
	}
	
	if (empty($errors)) {
		set_profile_info($_POST['username'],$_POST['email'], $_POST['description'], (empty($_FILES['image']['tmp_name'])) ? false : $_FILES['image']['tmp_name']); // if image has been uploaded, the tmp_name will be recorded.
	}
	
	$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" enctype ="multipart/form-data">
		<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>
			<label for="image">Image:</label>
			<input type="file" name="image" id="image"/>
		</div>
		<div>
			<input type = "submit" value = "Update" />
		</div>
	</form>
</body>
</html>
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: empty query

Post by Temor »

The error message isn't really an error message. It tells you it doesn't like you using a function as a parameter to a function.
 $file_ext = end(explode('.', $_FILES['image']['name']));
You can do one of two things:
1) Split the line into two:
 $file_ext = explode('.', $_FILES['image']['name']);
$file_ext = end($file_ext);
2) Turn off strict reporting by adding this to the top of your file:
ini_set('display_errors', '0');    
error_reporting(E_ALL | E_STRICT); 



When you upload an image, does it actually upload to the server? Can you browse to the image and view it without it being broken?
quanyii
Posts: 3
Joined: Thu Aug 15, 2013 9:49 am

Re: empty query

Post by quanyii »

Yes, I have gotten it fix. But how do I upload the image into my database? And how about the updating? Is it possible?
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: empty query

Post by Temor »

All of this is explained in the avatar / profile picture tutorials.
You don't actually upload the image to the database. You just save the location of the image ( example: C:\\Webstuff\images\quanyii.jpg ) and insert that into the database.
You then fetch that location when showing the image:
<img src="C:\\Webstuff\images\quanyii.jpg"/>
And yes, updating should be automatic.
If you upload one profile picture, and it's automatically named after the username for example, then the next time you upload a picture, it will overwrite the old one. The same url will show a different picture.


Like I said, all of this is covered in the tutorials ( I think ). If not, then I'll take my time and write a new tutorial and post it here.

Go through the youtube tutorials again to see what you missed, and if you're not happy after that, let me know.
Post Reply