Blog/Sign in/Email Activation/ = Problems

Post here is you are having problems with any of the tutorials.
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Blog/Sign in/Email Activation/ = Problems

Post by jaysus7 »

Hey guys wondering if you can he;lp i have watched your tutorials at least 10 times each and can't find what is stoping my code from working. i am extremely new and of course got myself in a project that i have no idea what I'm doing. i have learned lots and figured stuff out however these amazing life saving tutorials of yours i came a crossed just aren't working for me. so first problem

BLOG!

blog list error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/50/8811650/html/coreblog/initblog/posts.inc.php on line 43


blog read error:


Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/content/50/8811650/html/coreblog/initblog/posts.inc.php on line 8
Invalid post ID.

with the pid in the search bar, if i forget that part it just states invalid post!

posts.inc.php:

Fatal error: Cannot redeclare get_posts() (previously declared in /home/content/50/8811650/html/posts.inc.php:37) in /home/content/50/8811650/html/posts.inc.php on line 75

login in:

Username or Password incorrect! even though it is correct it won't log me in it just states that they are incorrect.

email validation:

won't actually activate the user, and it won't delete the activation code from the table???



My Code

Blog_list
[syntax=php]<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
include ("coreblog/initblog.php");

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php

$posts = get_posts();

foreach ($posts as $post){
	?>
    <h2><a href="blog_read.php?pid=<?php echo $post['id']; ?>"><?php echo $post['title']; ?></a></h2>
    <h4>By<?php echo $post['user']; ?> on <?php echo $post['date']; ?></h4>
    <h4>(<?php echo $post['total_comment'];?>comments, last comment<?php echo $post['last_comment']; ?>)</h4>
    
    <hr />
    
    <p><?php $post['preview']; ?></p>
    
    <?php
}

?>
</body>
</html>
[/syntax]

Blog_read
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
include ("coreblog/initblog.php");

if(isset($_GET['pid'], $_POST['user'], $POST['body'])){

	if (add_comment($GET['pid'], $_POST['user'], $_POST['body'])){
		header("Location: blog_read.php?pid={$_GET['pid']}");
	}else{
		header('Location: blog_list.php');
	}
	die();
}

?>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php

if (isset($_GET['pid']) === false || valid_pid($_GET['pid']) === false){
	echo 'Invalid post ID.';
}else{
	$post = get_post($_GET['pid']);

	?>
    <h2><?php echo $post['title']; ?></h2>
    <h4>By<?php echo $post['user']; ?> on <?php echo $post['date']; ?> (<?php echo count($post['comments']); ?> comments)</h4>
    
    <hr />
    
    <p><?php $post['body']; ?></p>
    
    <hr />
    <?php
	
	foreach($post['comments'] as $comment){
		?>
    
	 <h4>By<?php echo $comment['user']; ?> on <?php echo $comment['date']; ?></h4>
     <p><?php echo $comment['body']; ?></p>
     <hr />
     <?php
	}

	?>
<form action="" method="post">
<p>
	<label for="user">Name</label>
    <input type="text" name="user" id="user" />
</p>
<p>
	<textarea name="body" rows="20" collum="60"></textarea>
</p>
<p>
	<input type="submit" value="Add Comment" />
</p>
</form>    
<?php

}

?>
</body>
</html>

blog_posts

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
include ("coreblog/initblog.php");

if (isset($_POST['user'], $_POST['title'], $_POST['body'])){
	add_posts($_POST['user'], $_POST['title'], $_POST['body']);
	header('Location: blog_list.php');
	die();	
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="" method="post">
<p>
	<label for="user">Name</label>
    <input type="text" name="user" id="user" />
</p>
<p>
	<label for="user">Title </label>
    <input type="text" name="title" id="title" />
</p>
<p>
  <textarea name="body" rows="20" collums="60"></textarea>
</p>
<p>
	<input type="submit" value="Add Comment" />
</p>
</form>
</body>
</html>
posts.inc.php
<?php

//checks if the given post id is in the table
function valid_pid ($pid){
	$pid = (int)$pid;
	
	$total = mysql_query("SELECT COUNT(`post_id`) FROM `posts` WHERE `post_id` {$pid}");
	$total = mysql_result($total, 0);
	ini_set('display_errors',1);
error_reporting(E_ALL);
	if ($total  != 1){
		return false;
	}else{
		return true;
	}
}

//festches a summery of all the blog posts
function get_posts(){
	$sql = "SELECT
				`posts`.`post_id` AS `id`,
				`posts`.`post_title` AS `title`,
				LEFT(`posts`.`post_body`, 512) AS `preview`,
				`posts`.`post_user` AS `user`,
				DATE_FORMAT(`posts`.`post_date`, '%d/%m/%Y %H:%i:%s') AS `date`,
				`comments`.`total_comments`,
				DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i:%s') AS `last_comment`
			FROM `posts`
			LEFT JOIN (
				 SELECT
				  `post_id`,
					COUNT(`comment_id`) AS `title_comments`,
					MAX(`comment_date`) AS `last_comment`
				FROM `comments`
				GROUP BY `post_id`
			) AS `comments`
			ON `posts`.`post_id` = `comments`.`post_id`
			ORDER BY `posts`.`post_date` DESC";
	
	$posts = mysql_query($sql);
	
	$rows = array();
	while (($row = mysql_fetch_assoc($posts)) !== false){
		$rows[] = array(
			 'id'					=> $row['id'],
			 'title'				=> $row['title'],
			 'preview'				=> $row['preview'],
			 'user'					=> $row['user'],
			 'date'					=> $row['date'],
			 'total_comments'		=> ($row['total_comments'] === null) ? 0 : $row['total_comments'],
			 'last_comment'			=> ($row['last_comment'] === null) ? 'never' : $row['last_comment']
		);
		
		
	}
	return $rows;
}
ini_set('display_errors',1);
error_reporting(E_ALL);
//fetches a single post from the table
function get_post($pid){
	$pid = (int)$pid;
	
	$sql = "SELECT
			`post_title` AS `title`,
			`post_body` AS `body`,
			`post_user` AS `user`,
			`post_date` AS `date`
		FROM `posts`
		WHERE `post_id` = {$pid}";
		
	$post = mysql_query($sql);
	$post = mysql_fetch_assoc($post);
	
	$post['comments'] = get_comments($pid);
	
	return $post;
}

//adds new blog entry
function add_post($name, $title, $body){
	$name 	= mysql_real_escape_string(htmlentities($name));
	$title 	= mysql_real_escape_string(htmlentities($title));
	$body 	= mysql_real_escape_string(nl2br(htmlentities($body)));
	
	mysql_query("INSERT INTO `posts` (`post_user`, `post_title`, `post_body`, `post_date`) VALUES ('{$name}', '{$title}', '{$body}', NOW())");
	
}


?>


index

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
include('core/init.inc.php');

$errors = array();

if (isset($_POST['username'], $_POST['password'])){
	if (empty ($_POST['username'])){
		$errors[] = 'The username cannot be empty!';	
	}
	
	if (empty ($_POST['password'])){
		$errors[] = 'The password cannot be empty!';
	}
	
	if (valid_credentials($_POST['username'], sha1($_POST['password'])) === false){
		$errors[] = 'Username or Password incorrect!';
	}
	
	if (empty($errors) && is_active($_POST['username']) === false){
		$errors[] = 'This account has not yet been activated!';

	}
	
	if(empty($errors)){
	if (isset($_POST['set_cookie']) && $_POST['set_cookie'] == '1'){
		setcookie('username', $_POST['username'], time() + 604800);
		setcookie('password', sha1($_POST['password']), time() + 604800);
	}
	
	$_SESSION['username'] = htmlentities($_POST['username']);
	
	header('Location: protected.php');
	die();
	
	}
}

?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Login - Know Quest</title>
<style type="text/css">
<!--
body {
	font: 100%/1.4 Verdana, Arial, Helvetica, sans-serif;
	background: #42413C;
	margin: 0;
	padding: 0;
	color: #000;
	background-color: #FFFFFF;
}

/* ~~ Element/tag selectors ~~ */
ul, ol, dl { /* Due to variations between browsers, it's best practices to zero padding and margin on lists. For consistency, you can either specify the amounts you want here, or on the list items (LI, DT, DD) they contain. Remember that what you do here will cascade to the .nav list unless you write a more specific selector. */
	padding: 0;
	margin: 0;
}
h1, h2, h3, h4, h5, h6, p {
	margin-top: 0;	 /* removing the top margin gets around an issue where margins can escape from their containing div. The remaining bottom margin will hold it away from any elements that follow. */
	padding-right: 15px;
	padding-left: 15px; /* adding the padding to the sides of the elements within the divs, instead of the divs themselves, gets rid of any box model math. A nested div with side padding can also be used as an alternate method. */
	text-align: left;
}
a img { /* this selector removes the default blue border displayed in some browsers around an image when it is surrounded by a link */
	border: none;
}
/* ~~ Styling for your site's links must remain in this order - including the group of selectors that create the hover effect. ~~ */
a:link {
	color: #42413C;
	text-decoration: underline; /* unless you style your links to look extremely unique, it's best to provide underlines for quick visual identification */
}
a:visited {
	color: #6E6C64;
	text-decoration: underline;
}
a:hover, a:active, a:focus { /* this group of selectors will give a keyboard navigator the same hover experience as the person using a mouse. */
	text-decoration: none;
}

/* ~~ this fixed width container surrounds the other divs ~~ */
.container {
	width: 960px;
	background: #FFF;
	margin: 0 auto; /* the auto value on the sides, coupled with the width, centers the layout */
}

/* ~~ the header is not given a width. It will extend the full width of your layout. It contains an image placeholder that should be replaced with your own linked logo ~~ */
.header {
	background-color: #F00;
}

/* ~~ This is the layout information. ~~ 

1) Padding is only placed on the top and/or bottom of the div. The elements within this div have padding on their sides. This saves you from any "box model math". Keep in mind, if you add any side padding or border to the div itself, it will be added to the width you define to create the *total* width. You may also choose to remove the padding on the element in the div and place a second div within it with no width and the padding necessary for your design.

*/

.content {

	padding: 10px 0;
}

/* ~~ The footer ~~ */
.footer {
	padding: 10px 0;
	background-color: #ED1D23;
	color: #FFF;
	text-align: right;
}

/* ~~ miscellaneous float/clear classes ~~ */
.fltrt {  /* this class can be used to float an element right in your page. The floated element must precede the element it should be next to on the page. */
	float: right;
	margin-left: 8px;
}
.fltlft { /* this class can be used to float an element left in your page. The floated element must precede the element it should be next to on the page. */
	float: left;
	margin-right: 8px;
}
.clearfloat { /* this class can be placed on a <br /> or empty div as the final element following the last floated div (within the #container) if the #footer is removed or taken out of the #container */
	clear:both;
	height:0;
	font-size: 1px;
	line-height: 0px;
}
.container .header {
	text-align: center;
}
.container .content table {
	text-align: left;
}
.container .footer h5 {
	text-align: left;
}
.container .content {
	text-align: left;
}
.tt {
	text-align: center;
}
.container .footer {
	text-align: right;
}
.container .footer h6 {
	text-align: right;
}
.container .content form label {
	text-align: left;
}
.container .content h5 {
	text-align: center;
}
.container .content {
	text-align: center;
}
-->
</style></head>

<body>

<div class="container">
  <div class="header"><!-- end .header -->
    <p> </p>
    <p><img src="Images/Logo.gif" alt="Logo" width="807" height="207" align="left" /></p>
    <p> </p>
    <p> </p>
    <p> </p>
    <p> </p>
    <p> </p>
    <p> </p>
  </div>
  <div class="content">
    <div>
	<?php
    if (empty($errors) === false){
	?>
    <ul>
    <?php
	foreach ($errors as $error){
		echo "<li>{$error}</li>";
		}
	?>
    </ul>
    <?php
    }else{
		echo 'Need an account? <a href="sign up1.php">Sign Up </a>';
		}
	?>
    </div>
    </p>
	
	</h5>
    <p>
    </p>
    <form action="" method="post" name="login">
  <table width="100%" border="0">
      <tr>
        <td width="14%">Username:</td>
        <td width="86%"><input type="username" name="username"  value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['$username']); ?>"></td>
      </tr>
      <tr>
        <td>Password:</td>
        <td><input type="password" name="password"></td>
      </tr>
         <tr>
        <td><label for="set_cookie" >Remember me:</label></td>
        <td><input type="checkbox" name="set_cookie" id="set_cookie" value="1"/></td>
      </tr>
     
  </table>
   
  <p><br>
    <input type="submit" name="submit" value="Login"><br>
  </p>
  <p> </p>
  <p> </p>
</form>
    
  </div>
  <div class="footer">    <!-- end .footer -->
    <h6> </h6>
    <form>
Search
      <input type="text" size="30" onKeyUp="showResult(this.value)" />
      
      <h5>_______________________________________________________________________________________________________</h5>
      <h5>
        <!-- end .footer -->KnowQuest © 2012 English (Canada) | Terms of use | Search | Prof. Finder | Ratings | Your Research </h5>
    </form>
  </div>
  <!-- end .container --></div>
</body>
</html>

user.inc.php

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
//fetches all users from the user 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){
	$user[] = $row;	
	}
	
	return $users;

}
//fetches user profile information
function fetch_user_info($uid){

	$uid = (int)$uid;

	$sql = "SELECT
				`user_id` AS `id`,
				`user_username` AS `username`,
				`user_firstname` AS `firstname`,
				`user_lastname` AS `lastname`,
				`user_institution` AS `institution`,
				`user_about institution` AS `aboutinstitution`,
				`user_professional title` AS `professionaltitle`,
				`user_professional research` AS `professionalresearch`,
				`user_prefessional website` AS `professionalwebsite`,
				`user_personal website` AS `personalwebsite`,
				`user_personal email` AS `personalemail`,
				`user_professional email` AS `professionalemail`,
				`user_about your research` AS `aboutyourresearch`,
				`user_about yourself` AS `aboutyourself`,
				`user_social media` AS `socialmedia`, 
				FROM `users`
				WHERE `user_id` = {$uid}";
	
	$result = mysql_query($sql);
	
	$info = mysql_fetch_assoc($result);
	
	$info['pictureid'] = (file_exists("{GLOBALS['$path']}/user_portfolio picture{$info['id']}.jpg"))  ? "core/user_portfolio picture{$info['id']}.jpg" : "core/user_portfolio picture/default.jpg";
	
	return $info;
	
}

//updates the user infos profile info

function set_profile_info($professonalwebsite, $personalwebsite, $institution, $aboutinstitution, $professionalresearch, $professionaltitle, $professionalemail, $aboutyourresearch, $aboutyourself, $socialmedia, $personalemail, $pictureid){

	$professinalemail		= mysql_real_escape_string(htmlentities($professionalemail));
	$personalemail			= mysql_real_escape_string(htmlentities($personalemail));
	$personalwebsite		= mysql_real_escape_string(htmlentities($personalwebsite));
	$professonalwebsite		= mysql_real_escape_string(htmlentities($professonalwebsite));
	$institution			= mysql_real_escape_string(htmlentities($institution));
	$aboutinstitution		= mysql_real_escape_string(htmlentities(nl2br($aboutinstitution)));
	$professionalresearch	= mysql_real_escape_string(htmlentities($professionalresearch));
	$professionaltitle		= mysql_real_escape_string(htmlentities($professionaltitle));
	$aboutyourresearch		= mysql_real_escape_string(htmlentities(nl2br($aboutyourresearch)));
	$aboutyourself			= mysql_real_escape_string(htmlentities(nl2br($aboutyourself)));
	$socialmedia			= mysql_real_escape_string(htmlentities($socialmedia));

if (file_exists($pictureid)){
	$src_size = getimagesize($pictureid);
	
	if ($src_size['mime'] === 'image/jpeg'){
		$src_img = imagecreatefromjpeg($pictureid);
	}else if ($src_size['mime'] === 'image/png'){
		$src_img = imagecreatefrompng($pictureid);
	}else if ($src_size['mime'] === 'image/gif'){
		$src_img = imagecreatefromgif($pictureid);
	}else{ 
		$src_img = false;
	}
	
	if ($src_img !== false){
		$thumb_width = 200;
		
	if ($src_size[0] <= $thumb_width){
		$thumb = $src_img;
	}else{
		$new_size[0] = $thumb_width;
		$new_size[1] = ($src_size[1] / $src_size[0]) * $thumb_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_portfolio picture/{$SESSION['uid']}.jpg");
	
	}
	
}

	$sql = "UPDATE `users` SET
			`user_personal website` = '{$presonalwebsite}'
			`user_institution` = '{$institution}'
			`user_about institution` = '{$aboutinstitution}'
			`user_professional research` = '{$professionalresearch}'
			`user_professional title` = '{$professionaltitle}'
			`user_about your research` = '{$aboutyourresearch}'
			`user_about yourself` = '{$aboutyourself}'
			`user_personal email` = '{$personalemail}'
			`user_professional website` = '{$professionalwebsite}'
			`user_professional email` = '{$professionalemail}'
			`user_social media` = '{$socialmedia}'
			WHERE `user_id` = {$_SESSION['uid']}";
			
			mysql_query($sql);

}
//check if given username exsists in the database
function user_exists($user){
	$user = mysql_real_escape_string($user);
	
	$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_username` = '{$user}'");
	
	return (mysql_result($total, 0) == '1') ? true : false;
	
}
//check if the given username and password combinations are valid
function valid_credentials($user, $pass){
	$user = mysql_real_escape_string($user);
	$pass = mysql_real_escape_string($pass);
	
	$total= mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_username` = '{$user}' AND `user_password` = '{$pass}'");
	 
	return (mysql_result($total, 0) == '1') ? true : false;
	
}
// checks to see is user account is active
	function is_active($user){
		$user = mysql_real_escape_string($user);
		
		$sql = "SELECT
					COUNT (`activations`.`user_id`)
				FROM `users`
				INNER JOIN `activations`.`user_id`
				ON `users`.`user_id` = `activations`.`user_id`
				WHERE `users`.`user_username` = '{$user}'";
				
	$result = mysql_query($sql);
	
	return (mysql_result($result, 0) == '0') ? true : false;
	
		
}
//acctivates the account related to the given activation code
function activate_account($aid){
	$aid = mysql_real_escape_string($aid);
	
	mysql_query("DELETE FROM `activations` WHERE `activation_code` = '{$aid}'");

}

//adds a user to the database
function add_user($user, $email, $pass, $first, $last){
	$user		= mysql_real_escape_string(htmlentities($user));
	$email		= mysql_real_escape_string($email);
	$pass		= sha1($pass);
	$firstname		= mysql_real_escape_string(htmlentities($firstname));
	$lastname		= mysql_real_escape_string(htmlentities($lastname));
	
	$charset = array_flip(array_merge(range('a', 'z'), range('A', 'Z'), range('0', '9')));
	$aid =implode('', array_rand($charset, 10)); 
	
	$body =	<<<EMAIL
	
	Thank you for signing up with knowquest. To activate your account, please click the link below
	
	http://www.jasonmassieportfolio.com/activate.php?{$aid}
	
EMAIL;
	
	mail($email, 'Your new account at Knowquest.com', $body, 'From: admin@knowquest.com');
	
	mysql_query("INSERT INTO `users` (`user_username`, `user_email`, `user_password`) VALUES ('{$user}', '{$email}', '{$pass}')");
	
	$user_id = mysql_insert_id();
	
	mysql_query("INSERT INTO `activations` (`user_id`, `activation_code`) VALUES ({$user_id}, '{$aid}')");
	
}
?>


init.inc.php
<?php

session_start ();

$exceptions = array ('sign up1', 'index', 'activate', 'member1', 'Edit', 'user_lists', 'blog_posts');

$page = substr(end(explode('/', $_SERVER['SCRIPT_NAME'])), 0, -4);

mysql_connect("phplogin113.db.8811650.hostedresource.com","phplogin113","Hookups1");
mysql_select_db("phplogin113");

$path = dirname(__FILE__);

include ("{$path}/init/user.inc.php");




$_SESSION['uid'] = 1;

if (isset($_COOKIE['username'], $_COOKIE['passwords']) && isset($_SESSION['username']) === false){
	if (valid_credentials($_COOKIE['username'], $_COOKIE['password'])){
		$_SESSION['username'] = htmlentities($_COOKIE['username']);
	
		setcookie('username', $_COOKIE['username'], time() + 604800);
		setcookie('password', $_COOKIE['password'], time() + 604800);
	
	}

}

if (in_array($page, $exceptions) === false){
	if (isset($_SESSION['username']) === false){
		header('Location: index.php');
		die();
	}

}

?>
Last edited by jaysus7 on Sat Mar 03, 2012 9:40 pm, edited 2 times in total.
Just a helpless cause!!!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog/Sign in/Email Activation/ = Problems

Post by jacek »

jaysus7 wrote:and of course got myself in a project that i have no idea what I'm doing
Those are the most fun !

jaysus7 wrote:Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/50/8811650/html/coreblog/initblog/posts.inc.php on line 43
jaysus7 wrote:Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/content/50/8811650/html/coreblog/initblog/posts.inc.php on line 8
Invalid post ID.
Those mean that what every query is around that line is failing, usually it's a typo in the SQL. Try adding
echo mysql_error();
after the mysql_query() line and it should show you what the problem is.

jaysus7 wrote:Fatal error: Cannot redeclare get_posts() (previously declared in /home/content/50/8811650/html/posts.inc.php:37) in /home/content/50/8811650/html/posts.inc.php on line 75
you cant declare the same function more than once, remove one of them.

jaysus7 wrote:Username or Password incorrect! even though it is correct it won't log me in it just states that they are incorrect.
jaysus7 wrote:won't actually activate the user, and it won't delete the activation code from the table???
Try adding the echo mysql_error() after both queries and see if there is somethign that is stopping it working. Also make sure everything that goes into the conditions (WHERE part) for the queries has the value you expect.
Image
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Blog/Sign in/Email Activation/ = Problems

Post by jaysus7 »

thanks sorry bout the code... also nothin is showing up. however i am getting this error now

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/50/8811650/html/coreblog/initblog/posts.inc.php on line 46
<?php

//checks if the given post id is in the table
function valid_pid ($pid){
	$pid = (int)$pid;
	
	$total = mysql_query("SELECT COUNT(`post_id`) FROM `posts` WHERE `post_id` {$pid}");
	$total = mysql_result($total, 0);

	if ($total != 1){
		return false;
	}else{
		return true;
	}
}

//festches a summery of all the blog posts
function get_posts(){
	$sql = "SELECT
				`posts`.`post_id` AS `id`,
				`posts`.`post_title` AS `title`,
				LEFT(`posts`.`post_body`, 512) AS `preview`,
				`posts`.`post_user` AS `user`,
				DATE_FORMAT(`posts`.`post_date`, '%d/%m/%Y %H:%i:%s') AS `date`,
				`comments`.`total_comments`,
				DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i:%s') AS `last_comment`
			FROM `posts`
			LEFT JOIN (
				 SELECT
				  `post_id`,
					COUNT(`comment_id`) AS `title_comments`,
					MAX(`comment_date`) AS `last_comment`
				FROM `comments`
				GROUP BY `post_id`
			) AS `comments`
			ON `posts`.`post_id` = `comments`.`post_id`
			ORDER BY `posts`.`post_date` DESC";
	
	$posts = mysql_query($sql);
	
	$rows = array();
	
	while (($row = mysql_fetch_assoc($posts)) !== false){echo mysql_error();
		$rows[] = array(
			 'id'					=> $row['id'],
			 'title'				=> $row['title'],
			 'preview'				=> $row['preview'],
			 'user'					=> $row['user'],
			 'date'					=> $row['date'],
			 'total_comments'		=> ($row['total_comments'] === null) ? 0 : $row['total_comments'],
			 'last_comment'			=> ($row['last_comment'] === null) ? 'never' : $row['last_comment']
		);
		
	}
	return $rows;
}

//fetches a single post from the table
function get_post($pid){
	$pid = (int)$pid;
	
	$sql = "SELECT
			`post_title` AS `title`,
			`post_body` AS `body`,
			`post_user` AS `user`,
			`post_date` AS `date`
		FROM `posts`
		WHERE `post_id` = {$pid}";
		echo mysql_error();
	$post = mysql_query($sql);
	
	$post['comments'] = get_comments($pid);
	echo mysql_error();
	
	return $post;
}

//adds new blog entry
function add_post($name, $title, $body){
	$name 	= mysql_real_escape_string(htmlentities($name));
	$title 	= mysql_real_escape_string(htmlentities($title));
	$body 	= mysql_real_escape_string(nl2br(htmlentities($body)));
	
	mysql_query("INSERT INTO `posts` (`post_user`, `post_title`, `post_body`, `post_date`) VALUES ('{$name}', '{$title}', '{$body}', NOW())");
echo mysql_error();	
}


?>
Just a helpless cause!!!!
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Blog/Sign in/Email Activation/ = Problems

Post by jaysus7 »

Unknown column 'comments.total_comments' in 'field list'
for blog_list.php, however its spelt all correctly....

//check if the given username and password combinations are valid
function valid_credentials($user, $pass){
	$user = mysql_real_escape_string($user);
	$pass = mysql_real_escape_string($pass);
	
	$total= mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_username` = '{$user}' AND `user_password` = '{$pass}'");echo mysql_error();
	
	return (mysql_result($total, 0) == '1') ? true : false;
	
}

//festches a summery of all the blog posts
function get_posts(){
	$sql = "SELECT
				`posts`.`post_id` AS `id`,
				`posts`.`post_title` AS `title`,
				LEFT(`posts`.`post_body`, 512) AS `preview`,
				`posts`.`post_user` AS `user`,
				DATE_FORMAT(`posts`.`post_date`, '%d/%m/%Y %H:%i:%s') AS `date`,
				`comments`.`total_comments`,
				DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i:%s') AS `last_comment`
			FROM `posts`
			LEFT JOIN (
				 SELECT
				  `post_id`,
					COUNT(`comment_id`) AS `title_comments`,
					MAX(`comment_date`) AS `last_comment`
				FROM `comments`
				GROUP BY `post_id`
			) AS `comments`
			ON `posts`.`post_id` = `comments`.`post_id`
			ORDER BY `posts`.`post_date` DESC";
	
	$posts = mysql_query($sql);echo mysql_error();
	
	$rows = array();
	
	while (($row = mysql_fetch_assoc($posts)) !== false){echo mysql_error();
		$rows[] = array(
			 'id'					=> $row['id'],
			 'title'				=> $row['title'],
			 'preview'				=> $row['preview'],
			 'user'					=> $row['user'],
			 'date'					=> $row['date'],
			 'total_comments'		=> ($row['total_comments'] === null) ? 0 : $row['total_comments'],
			 'last_comment'			=> ($row['last_comment'] === null) ? 'never' : $row['last_comment']
		);
		
	}
	return $rows;
}



also nothing showed up for the login part or email activation....

i did fix the line 75 however and redeclared it post instead of posts
Just a helpless cause!!!!
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Blog/Sign in/Email Activation/ = Problems

Post by jaysus7 »

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/50/8811650/html/coreblog/initblog/comments.inc.php on line 16


I'm running mysql 5 and php 5 using go daddy as a host.... every time i put mysql_fetch_assoc it won't work why is this?
is there a different function i should be using????
$return = array();
	while (($row = mysql_fetch_assoc($comments)) !== false){
		$return[] = $row;
		
	}
		
	return $return;

never works for my websites.... what exactly does it do , how important is it and why doesn't it work...

i take it out of the code and basically everything works???
Just a helpless cause!!!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog/Sign in/Email Activation/ = Problems

Post by jacek »

jaysus7 wrote:what exactly does it do
http://php.net/manual/en/function.mysql-fetch-assoc.php
jaysus7 wrote:every time i put mysql_fetch_assoc it won't work why is this?
You get this error because the query you are trying to fetch the result of fails. That causes mysql_query() to return false (a boolean) that mysql_fetch_assoc() does not expect to see. you can see why the query failed by adding
echo mysql_error();
after it.
Image
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Blog/Sign in/Email Activation/ = Problems

Post by jaysus7 »

won't update the members page more check the email validate


edit page

<?php

include ("core.user/init.inc.user.php");

if (isset($_post['institution'], $_POST['aboutinstitution'], $_POST['professionaltitle'], $_POST['professionalresearch'], $_POST['professionalwerbsite'], $_POST['personalwebsite'], $_POST['personalemail'], $_POST['professionalemail'], $_POST['abotyourresearch'], $_POST['aboutyourself'], $_POST['socialmedia'])){
	$errors = array();

	if (filter_var($_POST['personalemail'], $_POST['professionalemail'], FILTER_VALIDATE_EMAIL) === false){
	$errors[] = 'The email address you entered is not valid.';

	}

	if (empty($errors)){
	set_profile_info($_POST['institution'], $_POST['aboutinstitution'], $_POST['professionaltitle'], $_POST['professionalresearch'], $_POST['professionalwerbsite'], $_POST['personalwebsite'], $_POST['personalemail'], $_POST['professionalemail'], $_POST['abotyourresearch'], $_POST['aboutyourself'], $_POST['socialmedia']);
	
	}

	$user_info = array(
				'institution' 			=> htmlentities($_POST['institution']),
				'aboutinstitution' 		=> htmlentities($_POST['aboutinstitution']),
				'professionaltitle'		=> htmlentities($_POST['professionaltitle']),
				'professionalresearch'	=> htmlentities($_POST['professionalresearch']),
				'professionalwebsite' 	=> htmlentities($_POST['professionalwebsite']),
				'personalwebsite'		=> htmlentities($_POST['personalwebsite']),
				'personalemail'			=> htmlentities($_POST['personalemail']),
				'professionalemail'		=> htmlentities($_POST['professionalemail']),
				'aboutyourresearch'		=> htmlentities($_POST['aboutyourresearch']),
				'aboutyourself'			=> htmlentities($_POST['aboutyourself']),
				'socialmedia'			=> htmlentities($_POST['socialmedia'])
	);
}else{	
	$user_info = fetch_user_info($_SESSION['uid']);
}
	
?>




	<?php
	
	if (isset($errors) === false){
		echo 'Click update to udate your portfolio.';
	}else if (empty($errors)){
		echo 'Your portfolio has been updated';
	}else{
		echo '<ul><li>', implode('</li><li>', $errors), '</li></ul>';
	}
	
	?>


user inc page

<?php

//fetches all of the users from the 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 info for given user
function fetch_user_info($uid){
	$uid = (int)$uid;
	
	$sql = "SELECT
				`user_username` AS `username`,
				`user_email` AS `email`,
				`user_firstname` AS `firstname`,
				`user_lastname` AS `lastname`,
				`user_institution` AS `institution`,
				`user_about institution` AS `aboutinstitution`,
				`user_professional title` AS `professionaltitle`,
				`user_professional research` AS `professionalresearch`,
				`user_professional website` AS `professionalwebsite`,
				`user_personal website` AS `personalwebsite`,
				`user_personal email` AS `personalemail`,
				`user_professional email` AS `professionalemail`,
				`user_about your research` AS `aboutyourresearch`,
				`user_about yourself` AS `aboutyourself`,
				`user_social media` AS `socialmedia`
			FROM`users`
			WHERE `user_id` = {$uid}";
			
			$result = mysql_query($sql);
			
			return mysql_fetch_assoc($result);
}

//updates current user portfolio info
function set_profile_info($institution, $aboutinstitution, $professionaltitle, $professionalresearch, $professionalwebsite, $presonalwebsite, $personalemail, $professionalemail, $aboutyourresearch, $aboutyourself, $socialmedia){
	$institution 			= mysql_real_escape_string(htmlentities($institution));
	$aboutinstitution 		= mysql_real_escape_string(nl2br(htmlentities($aboutinstitution)));
	$professionaltitle		= mysql_real_escape_string(htmlentities($professionaltitle));
	$professionalresearch 	= mysql_real_escape_string(htmlentities($professionalresearch));
	$professionalwebsite 	= mysql_real_escape_string(htmlentities($professionalwebsite));
	$personalwebsite 		= mysql_real_escape_string(htmlentities($personalwebsite));
	$personalemail			= mysql_real_escape_string(htmlentities($personalemail));
	$professionalemail 		= mysql_real_escape_string(htmlentities($professionalemail));
	$aboutyourresearch 		= mysql_real_escape_string(nl2br(htmlentities($aboutyourresearch)));
	$aboutyourself			= mysql_real_escape_string(nl2br(htmlentities($aboutyourself)));
	$socialmedia			= mysql_real_escape_string(htmlentities($socialmedia));

	$sql = "UPDATE `users` SET
			`user_institution` = '{$institution}',
			`user_about institution` = '{$aboutinstitution}',
			`user_professional title` = '{$professionaltitle}',
			`user_professional research` = '{$professionalresearch}',
			`user_professional website` = '{$professionalwebsite}',
			`user_personal website` = '{$presonalwebsite}',
			`user_personal email` = '{$personalemail}',
			`user_professional email` = '{$professionalemail}',
			`user_about your research` = '{$aboutyourresearch}',
			`user_about yourself` = '{$aboutyourself}',
			`user_social media` = '{$socialmedia}'
		WHERE `user_id` = {$SESSION['uid']}";
		
	mysql_query($sql);
		
}

?>
Just a helpless cause!!!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog/Sign in/Email Activation/ = Problems

Post by jacek »

What did the mysql_error() tell you ?
Image
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Blog/Sign in/Email Activation/ = Problems

Post by jaysus7 »

nothing thats the problem also doesn't check the errors from the edit page
Just a helpless cause!!!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog/Sign in/Email Activation/ = Problems

Post by jacek »

jaysus7 wrote:nothing thats the problem also doesn't check the errors from the edit page
Can you post the code with the mysql_error() added ? It should output something so you might have it in the wrong place.
Image
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Blog/Sign in/Email Activation/ = Problems

Post by jaysus7 »

user.inc.php
<?php

//fetches all of the users from the table
function fetch_users(){
	$result = mysql_query("SELECT `user_id` AS `id`, `user_username` AS `username` FROM `users`");	
	echo mysql_error();
	$users = array();
	
	while (($row = mysql_fetch_assoc($result)) !== false){
		$users[] = $row;
	}
	
	return $users;
}

//fetches profile info for given user
function fetch_user_info($uid){
	$uid = (int)$uid;
	
	$sql = "SELECT
				`user_id` AS `id`,
				`user_username` AS `username`,
				`user_email` AS `email`,
				`user_firstname` AS `firstname`,
				`user_lastname` AS `lastname`,
				`user_institution` AS `institution`,
				`user_about institution` AS `aboutinstitution`,
				`user_professional title` AS `professionaltitle`,
				`user_professional research` AS `professionalresearch`,
				`user_professional website` AS `professionalwebsite`,
				`user_personal website` AS `personalwebsite`,
				`user_personal email` AS `personalemail`,
				`user_professional email` AS `professionalemail`,
				`user_about your research` AS `aboutyourresearch`,
				`user_about yourself` AS `aboutyourself`,
				`user_social media` AS `socialmedia`
			FROM`users`
			WHERE `user_id` = {$uid}";
			
			$result = mysql_query($sql);
			echo mysql_error();
			$info = mysql_fetch_assoc($result);
			
			$info['avatar'] = "core.user/user_avatars/{$info['id']}.jpg";
			
			return $info;
}

//updates current user portfolio info
function set_profile_info($institution, $aboutinstitution, $professionaltitle, $professionalresearch, $professionalwebsite, $presonalwebsite, $personalemail, $professionalemail, $aboutyourresearch, $aboutyourself, $socialmedia, $avatar){
	$institution 			= mysql_real_escape_string(htmlentities($institution));
	$aboutinstitution 		= mysql_real_escape_string(nl2br(htmlentities($aboutinstitution)));
	$professionaltitle		= mysql_real_escape_string(htmlentities($professionaltitle));
	$professionalresearch 	= mysql_real_escape_string(htmlentities($professionalresearch));
	$professionalwebsite 	= mysql_real_escape_string(htmlentities($professionalwebsite));
	$personalwebsite 		= mysql_real_escape_string(htmlentities($personalwebsite));
	$personalemail			= mysql_real_escape_string(htmlentities($personalemail));
	$professionalemail 		= mysql_real_escape_string(htmlentities($professionalemail));
	$aboutyourresearch 		= mysql_real_escape_string(nl2br(htmlentities($aboutyourresearch)));
	$aboutyourself			= mysql_real_escape_string(nl2br(htmlentities($aboutyourself)));
	$socialmedia			= mysql_real_escape_string(htmlentities($socialmedia));

	if (file_exsists($avatar)){
		$src_size = getimagesize($avatar);
		
		if ($src_size['mime'] === 'image/jpeg'){
			$scr_img = imagecreatefromjpeg($avatar);
			
		}else if ($src_size['mime'] === 'image/png'){
				$scr_img = imagecreatefrompng($avatar);
			
		}else if ($src_size['mime'] === 'image/gif'){
				$scr_img = imagecreatefromgif($avatar);
		}else{ 
			$src_img = false;
		}
			
		if ($src_img !== false){
				$thumb_width = 200;
				
				if ($scr_size[0] <= $thumb_width){
					$thumb = $src_img;
				}else{ 
					$new_size[0] = $thumb_width;
					$new_size[1] = ($src_size[1] / $src_size[0]) * $thumb_width;
					
					$thumb = imagecreatetruecolor($new_size[0], $new_size[1]);
					imagecopyresampled($thumb, $src_img, 0, 0, 0, 0, $new_size[0], $src_size[0], $src_size[1]);
			}
			imagejpeg($thumb, "{GLOBALS['path']}/user_avatars/{$_SESSION['uid']}.jpeg");
		}
	}
	
	$sql = "UPDATE `users` SET
				`user_institution` = '{$institution}',
				`user_about institution` = '{$aboutinstitution}',
				`user_professional title` = '{$professionaltitle}',
				`user_professional research` = '{$professionalresearch}',
				`user_professional website` = '{$professionalwebsite}',
				`user_personal website` = '{$presonalwebsite}',
				`user_personal email` = '{$personalemail}',
				`user_professional email` = '{$professionalemail}',
				`user_about your research` = '{$aboutyourresearch}',
				`user_about yourself` = '{$aboutyourself}',
				`user_social media` = '{$socialmedia}'
			WHERE `user_id` = {$_SESSION['uid']}";
		
	mysql_query($sql);
	echo mysql_error();
		
}

?>

Edit

<?php

include ("core.user/init.inc.user.php");

if (isset($_POST['institution'], $_POST['aboutinstitution'], $_POST['professionaltitle'], $_POST['professionalresearch'], $_POST['professionalwerbsite'], $_POST['personalwebsite'], $_POST['personalemail'], $_POST['professionalemail'], $_POST['abotyourresearch'], $_POST['aboutyourself'], $_POST['socialmedia'])){
	$errors = array();

	if (filter_var($_POST['personal email'], $_POST['professional email'], FILTER_VALIDATE_EMAIL) === false){
	$errors[] = 'The email address you entered is not valid.';

	}
	
	if(empty($_FILES['avatars']['temp_name'])){
		$file_ext =	end(explode('.', $_FILES['avatars']['name']));
		
		if (in_array(strtolower($file_ext), array('jpg', 'jpeg', 'png','gif')) === false){
		$errors[] = 'your Picture id must be an image';
	}
	
}

	if (empty($errors)){
	set_profile_info($_POST['institution'], $_POST['aboutinstitution'], $_POST['professionaltitle'], $_POST['professionalresearch'], $_POST['professionalwebsite'], $_POST['personalwebsite'], $_POST['personalemail'], $_POST['professionalemail'], $_POST['abotyourresearch'], $_POST['aboutyourself'], $_POST['socialmedia'], (isset($_FILES['avatar']['temp_name'])) ? false : $_FILES['avatar']['temp_name']);
	
	}

	$user_info = array(
				'institution' 			=> htmlentities($_POST['institution']),
				'aboutinstitution' 		=> htmlentities($_POST['aboutinstitution']),
				'professionaltitle'		=> htmlentities($_POST['professionaltitle']),
				'professionalresearch'	=> htmlentities($_POST['professionalresearch']),
				'professionalwebsite' 	=> htmlentities($_POST['professionalwebsite']),
				'personalwebsite'		=> htmlentities($_POST['personalwebsite']),
				'personalemail'			=> htmlentities($_POST['personalemail']),
				'professionalemail'		=> htmlentities($_POST['professionalemail']),
				'aboutyourresearch'		=> htmlentities($_POST['aboutyourresearch']),
				'aboutyourself'			=> htmlentities($_POST['aboutyourself']),
				'socialmedia'			=> htmlentities($_POST['socialmedia'])
	);
}else{	
	$user_info = fetch_user_info($_GET['uid']);
}	
?>

<?php
	
	if (isset($errors) === false){
		echo 'Click update to update your portfolio.';
	}else if (empty($errors)){
		echo 'Your portfolio has been updated';
	}else{
		echo '<ul><li>', implode('</li><li>', $errors), '</li></ul>';
	}
	
	?>

<form action="" method="post" name="sign up" enctype="multitype/form-data">
  <table width="100%" border="0">
       <tr>
         <td width="23%"><label for='institution'>Institution:</label></td>
         <td width="77%"><input type="text" name="Institution" id="institution" value="<?php echo $user_info['institution']; ?>"></td>
       </tr>
    <tr>
        <td><label for='aboutinstituition'>About Instituition:</label></td>
        <td><textarea name"aboutinstitution" id="aboutinstitution" rows="15" cols="50"><?php echo strip_tags($user_info['aboutinstitution']); ?></textarea></td>
      </tr>
      <tr>
        <td><label for='professionaltitle'>Professional Title:</label></td>
        <td><input type="text" name="professionaltitle" id="professionaltitle" value="<?php echo $user_info['professionaltitle']; ?>"></td>
      </tr>
      <tr>
        <td><label for='professionalresearch'>Professional Research:</label></td>
        <td><input type="textarea" name="professionalresearch" id="professionalresearch" value="<?php echo $user_info['professionalresearch']; ?>"></td>
      </tr>
      <tr>
        <td><label for='professionalwebsite:'>Professional Website:</label></td>
        <td><input type="text" name="Professional Website" id="" value="<?php echo $user_info['professionalwebsite']; ?>"></td>
      </tr>
       <tr>
        <td width="23%"><label for='personalwebsites'>Personal Websites:</label></td>
        <td width="77%"><input type="text" name="personalwebsites" id="personalwebsites" value="<?php echo $user_info['persosnalwebsite']; ?>"></td>
    </tr>
    <tr>
        <td><label for='personalemail'>personal Email:</label></td>
        <td><input type="text" name="personalemail"id="personalemail"  value="<?php echo $user_info['persinalemail']; ?>"></td>
      </tr>
      <tr>
        <td><label for='professionalemail'>Professional Email:</label></td>
        <td><input type="text" name="professionalemail" id="professionalemail" value="<?php echo $user_info['professionalemail']; ?>"></td>
      </tr>
      <tr>
        <td><label for='aboutyourresearch'>About your Research:</label></td>
        <td><textarea name"aboutyourresearch" id="aboutyourresearch" rows="15" cols="50"><?php echo strip_tags($user_info['aboutyourresearch']); ?></textarea></td>
      </tr>
      <tr>
        <td><label for='aboutyourself'>Autobiography:</label></td>
        <td><textarea name"aboutyourself" id="aboutyourself" rows="15" cols="50"><?php echo strip_tags($user_info['aboutyourself']); ?></textarea></td>
      </tr>
      <tr>
        <td><label for='socialmedia'>Social Networking:</label></td>
        <td><input type="text" name="socialmedia" id="socialmedia" value="<?php echo $user_info['socialmedia']; ?>"></td>
      </tr>
      <tr>
        <td><label for='avatar'>Picture Id:</label></td>
        <td><input type="file" name="avatar" id="avatar" value=""/></td>
      </tr>
  </table>
  <p><br><br>
    <input type="submit" name="submit" value="Update">
  </p>
  
    </form>
Just a helpless cause!!!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog/Sign in/Email Activation/ = Problems

Post by jacek »

That looks fine ! which means it must be that the isset line at the very top of the page is not returning true. After about an hour of starting at it I spotted this
<td><textarea name"aboutyourself" id="aboutyourself" rows="15" cols="50"><?php echo strip_tags($user_info['aboutyourself']); ?></textarea></td>
There is an = missing after the name attribute here. If that isn't it, check all of the names to make sure they match the ones you use at the top.
Image
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Blog/Sign in/Email Activation/ = Problems

Post by Temor »

There are a couple of typos, $_POST['professionalwerbsite'] for example, and $_POST['abotyourresearch'].

you should clean your code a bit, make it easier to read and then thoroughly go through it for typos.
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Blog/Sign in/Email Activation/ = Problems

Post by jaysus7 »

ok spell checked everything!!!!! still nothing.....

also avatar isn't working....

however i am starting to understand php a little more which is better
edit
<?php

include ("core.user/init.inc.user.php");

if (isset($_POST['institution'], $_POST['aboutinstitution'], $_POST['professionaltitle'], $_POST['professionalresearch'], $_POST['professionalwebsite'], $_POST['personalwebsite'], $_POST['personalemail'], $_POST['professionalemail'], $_POST['aboutyourresearch'], $_POST['aboutyourself'], $_POST['socialmedia'])){
	$errors = array();

	if (filter_var($_POST['personalemail'], $_POST['professionalemail'], FILTER_VALIDATE_EMAIL) === false){
	$errors[] = 'The email address you entered is not valid.';

	}
	
	if (empty($_FILES['avatar']['tmp_name']) === false){
		$file_ext =	end(explode('.', $_FILES['avatar']['name']));
		
		if (in_array(strtolower($file_ext), array('jpg', 'jpeg', 'png', 'gif')) === false){
			$errors[] = 'your Picture id must be an image';
	}
	
}

	if (empty($errors)){
	set_profile_info($_POST['institution'], $_POST['aboutinstitution'], $_POST['professionaltitle'], $_POST['professionalresearch'], $_POST['professionalwebsite'], $_POST['personalwebsite'], $_POST['personalemail'], $_POST['professionalemail'], $_POST['abotyourresearch'], $_POST['aboutyourself'], $_POST['socialmedia'], (empty($_FILES['avatar']['tmp_name'])) ? false : $_FILES['avatar']['tmp_name']);
	
	}

	$user_info = array(
		'institution' 			=> htmlentities($_POST['institution']),
		'aboutinstitution' 		=> htmlentities($_POST['aboutinstitution']),
		'professionaltitle'		=> htmlentities($_POST['professionaltitle']),
		'professionalresearch'	=> htmlentities($_POST['professionalresearch']),
		'professionalwebsite' 	=> htmlentities($_POST['professionalwebsite']),
		'personalwebsite'		=> htmlentities($_POST['personalwebsite']),
		'personalemail'			=> htmlentities($_POST['personalemail']),
		'professionalemail'		=> htmlentities($_POST['professionalemail']),
		'aboutyourresearch'		=> htmlentities($_POST['aboutyourresearch']),
		'aboutyourself'			=> htmlentities($_POST['aboutyourself']),
		'socialmedia'			=> htmlentities($_POST['socialmedia'])
	);
}else{	
	$user_info = fetch_user_info($_SESSION['uid']);
}	
?>

<?php
	
	if (isset($errors) === false){
		echo 'Click update to update your portfolio.';
	}else if (empty($errors)){
		echo 'Your portfolio has been updated';
	}else{
		echo '<ul><li>', implode('</li><li>', $errors), '</li></ul>';
	}
	
	?>

<form action="" method="post" enctype="multitype/form-data">
  <table width="100%" border="0">
       <tr>
         <td width="23%"><label for='institution'>Institution:</label></td>
         <td width="77%"><input type="text" name="Institution" id="institution" value="<?php echo $user_info['institution']; ?>"></td>
       </tr>
    <tr>
        <td><label for='aboutinstituition'>About Instituition:</label></td>
        <td><textarea name="aboutinstitution" id="aboutinstitution" rows="15" cols="50"><?php echo strip_tags($user_info['aboutinstitution']); ?></textarea></td>
      </tr>
      <tr>
        <td><label for='professionaltitle'>Professional Title:</label></td>
        <td><input type="text" name="professionaltitle" id="professionaltitle" value="<?php echo $user_info['professionaltitle']; ?>"></td>
      </tr>
      <tr>
        <td><label for='professionalresearch'>Professional Research:</label></td>
        <td><input type="text" name="professionalresearch" id="professionalresearch" value="<?php echo $user_info['professionalresearch']; ?>"></td>
      </tr>
      <tr>
        <td><label for='professionalwebsite:'>Professional Website:</label></td>
        <td><input type="text" name="professionalwebsite" id="professionalwebsite" value="<?php echo $user_info['professionalwebsite']; ?>"></td>
      </tr>
       <tr>
        <td width="23%"><label for='personalwebsite'>Personal Website:</label></td>
        <td width="77%"><input type="text" name="personalwebsite" id="personalwebsite" value="<?php echo $user_info['personalwebsite']; ?>"></td>
    </tr>
    <tr>
        <td><label for='personalemail'>personal Email:</label></td>
        <td><input type="text" name="personalemail"id="personalemail"  value="<?php echo $user_info['personalemail']; ?>"></td>
      </tr>
      <tr>
        <td><label for='professionalemail'>Professional Email:</label></td>
        <td><input type="text" name="professionalemail" id="professionalemail" value="<?php echo $user_info['professionalemail']; ?>"></td>
      </tr>
      <tr>
        <td><label for='aboutyourresearch'>About your Research:</label></td>
        <td><textarea name="aboutyourresearch" id="aboutyourresearch" rows="15" cols="50"><?php echo strip_tags($user_info['aboutyourresearch']); ?></textarea></td>
      </tr>
      <tr>
        <td><label for='aboutyourself'>Autobiography:</label></td>
        <td><textarea name="aboutyourself" id="aboutyourself" rows="15" cols="50"><?php echo strip_tags($user_info['aboutyourself']); ?></textarea></td>
      </tr>
      <tr>
        <td><label for='socialmedia'>Social Networking:</label></td>
        <td><input type="text" name="socialmedia" id="socialmedia" value="<?php echo $user_info['socialmedia']; ?>"></td>
      </tr>
      <tr>
        <td><label for='avatar'>Picture Id:</label></td>
        <td><input type="file" name="avatar" id="avatar" value=""/></td>
      </tr>
  </table>
  <p><br><br>
    <input type="submit" value="Update">
  </p>
  
    </form>

user

<?php
 
//fetches all of the users from the table
function fetch_users(){
        $result = mysql_query("SELECT `user_id` AS `id`, `user_username` AS `username` FROM `users`"); 
        echo mysql_error();
        $users = array();
       
        while (($row = mysql_fetch_assoc($result)) !== false){
                $users[] = $row;
        }
       
        return $users;
}
 
//fetches profile info for given user
function fetch_user_info($uid){
        $uid = (int)$uid;
       
        $sql = "SELECT
                                `user_id` AS `id`,
                                `user_username` AS `username`,
                                `user_email` AS `email`,
                                `user_firstname` AS `firstname`,
                                `user_lastname` AS `lastname`,
                                `user_institution` AS `institution`,
                                `user_about institution` AS `aboutinstitution`,
                                `user_professional title` AS `professionaltitle`,
                                `user_professional research` AS `professionalresearch`,
                                `user_professional website` AS `professionalwebsite`,
                                `user_personal website` AS `personalwebsite`,
                                `user_personal email` AS `personalemail`,
                                `user_professional email` AS `professionalemail`,
                                `user_about your research` AS `aboutyourresearch`,
                                `user_about yourself` AS `aboutyourself`,
                                `user_social media` AS `socialmedia`
                        FROM`users`
                        WHERE `user_id` = {$uid}";
                       
                        $result = mysql_query($sql);
                        echo mysql_error();
                        $info = mysql_fetch_assoc($result);
                       
                        $info['avatar'] = "core.user/user_avatars/{$info['id']}.jpg";
                       
                        return $info;
}
 
//updates current user portfolio info
function set_profile_info($institution, $aboutinstitution, $professionaltitle, $professionalresearch, $professionalwebsite, $presonalwebsite, $personalemail, $professionalemail, $aboutyourresearch, $aboutyourself, $socialmedia, $avatar){
        $institution                    = mysql_real_escape_string(htmlentities($institution));
        $aboutinstitution               = mysql_real_escape_string(nl2br(htmlentities($aboutinstitution)));
        $professionaltitle              = mysql_real_escape_string(htmlentities($professionaltitle));
        $professionalresearch   		= mysql_real_escape_string(htmlentities($professionalresearch));
        $professionalwebsite    		= mysql_real_escape_string(htmlentities($professionalwebsite));
        $personalwebsite                = mysql_real_escape_string(htmlentities($personalwebsite));
        $personalemail                  = mysql_real_escape_string(htmlentities($personalemail));
        $professionalemail              = mysql_real_escape_string(htmlentities($professionalemail));
        $aboutyourresearch              = mysql_real_escape_string(nl2br(htmlentities($aboutyourresearch)));
        $aboutyourself                  = mysql_real_escape_string(nl2br(htmlentities($aboutyourself)));
        $socialmedia                    = mysql_real_escape_string(htmlentities($socialmedia));
 
        if (file_exsists($avatar)){
                $src_size = getimagesize($avatar);
               
                if ($src_size['mime'] === 'image/jpeg'){
                        $scr_img = imagecreatefromjpeg($avatar);
                       
                }else if ($src_size['mime'] === 'image/png'){
                                $scr_img = imagecreatefrompng($avatar);
                       
                }else if ($src_size['mime'] === 'image/gif'){
                                $scr_img = imagecreatefromgif($avatar);
                }else{
                        $src_img = false;
                }
                       
                if ($src_img !== false){
                                $thumb_width = 200;
                               
                                if ($scr_size[0] <= $thumb_width){
                                        $thumb = $src_img;
                                }else{
                                        $new_size[0] = $thumb_width;
                                        $new_size[1] = ($src_size[1] / $src_size[0]) * $thumb_width;
                                       
                                        $thumb = imagecreatetruecolor($new_size[0], $new_size[1]);
                                        imagecopyresampled($thumb, $src_img, 0, 0, 0, 0, $new_size[0], $src_size[0], $src_size[1]);
                        }
                        imagejpeg($thumb, "{GLOBALS['path']}/user_avatars/{$_SESSION['uid']}.jpeg");
                }
        }
       
        $sql = "UPDATE `users` SET
                                `user_institution` = '{$institution}',
                                `user_about institution` = '{$aboutinstitution}',
                                `user_professional title` = '{$professionaltitle}',
                                `user_professional research` = '{$professionalresearch}',
                                `user_professional website` = '{$professionalwebsite}',
                                `user_personal website` = '{$presonalwebsite}',
                                `user_personal email` = '{$personalemail}',
                                `user_professional email` = '{$professionalemail}',
                                `user_about your research` = '{$aboutyourresearch}',
                                `user_about yourself` = '{$aboutyourself}',
                                `user_social media` = '{$socialmedia}'
                        WHERE `user_id` = {$_SESSION['uid']}";
               
        mysql_query($sql);
        echo mysql_error();
               
}
 
?>
Last edited by jaysus7 on Mon Mar 19, 2012 5:14 am, edited 1 time in total.
Just a helpless cause!!!!
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Blog/Sign in/Email Activation/ = Problems

Post by jaysus7 »

The errors aren't showing up which i think is the problem....the click update to update your research and your research zhas been updated, the email authentication email ones....


ok i put the error_reporting(E_ALL); and i got all undefined index for all the user_info....so I'm trying to figure that out ..... thanks for all your help so far by the way !!!
Just a helpless cause!!!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog/Sign in/Email Activation/ = Problems

Post by jacek »

Well the undefined index messages will tell you which ones are wrong. This happens all the time with big forms you just have to keep fixing them until it works.
Image
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Blog/Sign in/Email Activation/ = Problems

Post by jaysus7 »

I've been looking at other forums about setting the session uid, however it is not working for me, and i have put my own information in but nothing works it set the session uid. also the errors won't work, nothing with the avatar tutorial works either and i really don't know why??? please help....sorry for all the stupid question, but i am trying to solve things on my own. i realized y the unassigned indexes were showing up.


init.user.php
<?php
//fetches the current logged in users id
function fetch_current_user_id($username){
	
$username = mysql_real_escape_string($username);

$sql = "SELECT `user_id` FROM `users` WHERE `user_username` = '{$username}'";
 
$result = mysql_query($sql);
 
return mysql_result($result, 0);
 
}
 
if (empty($_SESSION['uid'])){
		$_SESSION['uid'] = fetch_current_user_id($_SESSION['username']);
}
//check if given username exsists in the database
function user_exists($user){
	$user = mysql_real_escape_string($user);
	
	$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_username` = '{$user}'");
	echo mysql_error();
	return (mysql_result($total, 0) == '1') ? true : false;

}
//check if the given username and password combinations are valid
function valid_credentials($user, $pass){
	$user = mysql_real_escape_string($user);
	$pass = mysql_real_escape_string($pass);
	
	$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_username` = '{$user}' AND `user_password` = '{$pass}'");
	echo mysql_error();
	return (mysql_result($total, 0) == '1') ? true : false;
	
}
// checks to see is user account is active
	function is_active($user){
		$user = mysql_real_escape_string($user);
		echo mysql_error();
		$sql = "SELECT
					COUNT(`activations`.`user_id`)
				FROM `users`
				INNER JOIN `activations`
				ON `users`.`user_id` = `activations`.`user_id`
				WHERE `users`.`user_username` = '{$user}'";
				
	$result = mysql_query($sql);
	echo mysql_error();
	return (mysql_result($result, 0) == '0') ? true : false;
		
}
//acctivates the account related to the given activation code
function activate_account($aid){
	$aid = mysql_real_escape_string($aid);
	
	mysql_query("DELETE FROM `activations` WHERE `activation_code` = '{$aid}'");
echo mysql_error();
}

//adds a user to the database
function add_user($user, $email, $pass, $first, $last){
	$user		= mysql_real_escape_string(htmlentities($user));
	$email		= mysql_real_escape_string($email);
	$pass		= sha1($pass);
	$first		= mysql_real_escape_string(htmlentities($first));
	$last		= mysql_real_escape_string(htmlentities($last));
	
	$charset = array_flip(array_merge(range('a', 'z'), range('A', 'Z'), range('0', '9')));
	$aid =implode('', array_rand($charset, 10)); 
	
	$body =	<<<EMAIL
	
	Thank you for signing up with knowquest. To activate your account, please click the link below
	
	http://www.jasonmassieportfolio.com/act ... ?aid={$aid}
	
EMAIL;
	
	mail($email, 'Your new account at Knowquest.com', $body, 'From: admin@knowquest.com');
	
	mysql_query("INSERT INTO `users` (`user_username`, `user_email`, `user_password`,`user_firstname`,`user_lastname`) VALUES ('{$user}', '{$email}', '{$pass}','{$first}','{$last}')");
	echo mysql_error();
	$user_id = mysql_insert_id();
	
	mysql_query("INSERT INTO `activations` (`user_id`, `activation_code`) VALUES ({$user_id}, '{$aid}')");
echo mysql_error();
}
?>
init.php
<?php
error_reporting(E_ALL);

session_start ();

mysql_connect("","","");
mysql_select_db("phplogin113");

$path = dirname(__FILE__);

include ("{$path}/init.user/user.inc.user.php");

$_SESSION['uid'] = 59;

?>
edit
<?php
error_reporting(E_ALL);
include ("core.user/init.inc.user.php");

if (isset($_POST['institution'], $_POST['aboutinstitution'], $_POST['professionaltitle'], $_POST['professionalresearch'], $_POST['professionalwebsite'], $_POST['personalwebsite'], $_POST['personalemail'], $_POST['professionalemail'], $_POST['aboutyourresearch'], $_POST['aboutyourself'], $_POST['socialmedia'])){
	$errors = array();

	if (filter_var($_POST['personalemail'], $_POST['professionalemail'], FILTER_VALIDATE_EMAIL) === false){
	$errors[] = 'The email address you entered is not valid.';

	}
	
	if (empty($_FILES['avatar']['tmp_name']) === false){
		$file_ext =	end(explode('.', $_FILES['avatar']['name']));
		
		if (in_array(strtolower($file_ext), array('jpg', 'jpeg', 'png', 'gif')) === false){
			$errors[] = 'your Picture id must be an image';
	}
	
}

	if (empty($errors)){
	set_profile_info($_POST['institution'], $_POST['aboutinstitution'], $_POST['professionaltitle'], $_POST['professionalresearch'], $_POST['professionalwebsite'], $_POST['personalwebsite'], $_POST['personalemail'], $_POST['professionalemail'], $_POST['abotyourresearch'], $_POST['aboutyourself'], $_POST['socialmedia'], (empty($_FILES['avatar']['tmp_name'])) ? false : $_FILES['avatar']['tmp_name']);
	
	}

	$user_info = array(
		'institution' 			=> htmlentities($_POST['institution']),
		'aboutinstitution' 		=> htmlentities($_POST['aboutinstitution']),
		'professionaltitle'		=> htmlentities($_POST['professionaltitle']),
		'professionalresearch'	=> htmlentities($_POST['professionalresearch']),
		'professionalwebsite' 	=> htmlentities($_POST['professionalwebsite']),
		'personalwebsite'		=> htmlentities($_POST['personalwebsite']),
		'personalemail'			=> htmlentities($_POST['personalemail']),
		'professionalemail'		=> htmlentities($_POST['professionalemail']),
		'aboutyourresearch'		=> htmlentities($_POST['aboutyourresearch']),
		'aboutyourself'			=> htmlentities($_POST['aboutyourself']),
		'socialmedia'			=> htmlentities($_POST['socialmedia'])
	);
}else{	
	$user_info = fetch_user_info($_SESSION['uid']);
}	
?>

<?php
	
	if (isset($errors) === false){
		echo 'Click update to Edit your portfolio.';
	}else if (empty($errors)){
		echo 'Your portfolio has been updated';
	}else{
		echo '<ul><li>', implode('</li><li>', $errors), '</li></ul>';
	}
	
	?>

<form action="" method="post" enctype="multitype/form-data">
  <table width="100%" border="0">
       <tr>
         <td width="23%"><label for='institution'>Institution:</label></td>
         <td width="77%"><input type="text" name="Institution" id="institution" value="<?php echo $user_info['institution']; ?>"></td>
       </tr>
    <tr>
        <td><label for='aboutinstituition'>About Instituition:</label></td>
        <td><textarea name="aboutinstitution" id="aboutinstitution" rows="15" cols="50"><?php echo strip_tags($user_info['aboutinstitution']); ?></textarea></td>
      </tr>
      <tr>
        <td><label for='professionaltitle'>Professional Title:</label></td>
        <td><input type="text" name="professionaltitle" id="professionaltitle" value="<?php echo $user_info['professionaltitle']; ?>"></td>
      </tr>
      <tr>
        <td><label for='professionalresearch'>Professional Research:</label></td>
        <td><input type="text" name="professionalresearch" id="professionalresearch" value="<?php echo $user_info['professionalresearch']; ?>"></td>
      </tr>
      <tr>
        <td><label for='professionalwebsite:'>Professional Website:</label></td>
        <td><input type="text" name="professionalwebsite" id="professionalwebsite" value="<?php echo $user_info['professionalwebsite']; ?>"></td>
      </tr>
       <tr>
        <td width="23%"><label for='personalwebsite'>Personal Website:</label></td>
        <td width="77%"><input type="text" name="personalwebsite" id="personalwebsite" value="<?php echo $user_info['personalwebsite']; ?>"></td>
    </tr>
    <tr>
        <td><label for='personalemail'>personal Email:</label></td>
        <td><input type="text" name="personalemail"id="personalemail"  value="<?php echo $user_info['personalemail']; ?>"></td>
      </tr>
      <tr>
        <td><label for='professionalemail'>Professional Email:</label></td>
        <td><input type="text" name="professionalemail" id="professionalemail" value="<?php echo $user_info['professionalemail']; ?>"></td>
      </tr>
      <tr>
        <td><label for='aboutyourresearch'>About your Research:</label></td>
        <td><textarea name="aboutyourresearch" id="aboutyourresearch" rows="15" cols="50"><?php echo strip_tags($user_info['aboutyourresearch']); ?></textarea></td>
      </tr>
      <tr>
        <td><label for='aboutyourself'>Autobiography:</label></td>
        <td><textarea name="aboutyourself" id="aboutyourself" rows="15" cols="50"><?php echo strip_tags($user_info['aboutyourself']); ?></textarea></td>
      </tr>
      <tr>
        <td><label for='socialmedia'>Social Networking:</label></td>
        <td><input type="text" name="socialmedia" id="socialmedia" value="<?php echo $user_info['socialmedia']; ?>"></td>
      </tr>
      <tr>
        <td><label for='avatar'>Picture Id:</label></td>
        <td><input type="file" name="avatar" id="avatar" value=""/></td>
      </tr>
  </table>
  <p><br><br>
    <input type="submit" value="Update">
  </p>
  
    </form>
Last edited by jacek on Wed Mar 21, 2012 2:44 pm, edited 1 time in total.
Reason: Removed database password.
Just a helpless cause!!!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog/Sign in/Email Activation/ = Problems

Post by jacek »

if (empty($_SESSION['uid'])){
                $_SESSION['uid'] = fetch_current_user_id($_SESSION['username']);
}
This should be in the init file in place of
$_SESSION['uid'] = 59;
Image
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Blog/Sign in/Email Activation/ = Problems

Post by jaysus7 »

LIFE SAVER!!!!! thanks man !!!
Just a helpless cause!!!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog/Sign in/Email Activation/ = Problems

Post by jacek »

jaysus7 wrote:LIFE SAVER!!!!! thanks man !!!
I take it that means it worked ? If so HOORAY !
Image
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Blog/Sign in/Email Activation/ = Problems

Post by jaysus7 »

The only thing now that I need is why my errors on the edit page don't work....I'm quite proud that I figured some stuff on my own...with your help of course however I still can't edit my avatar and the users info....which is the most important thing i need right now....im going to over look everything once again though so i am trying to learn this stuff which i am...and login it!!!!!


Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 4 in /home/content/50/8811650/html/core/init/user.inc.php on line 12


might be the problem it won't jump to the row i am asking it to jump too...i don't know what to do with that?....


User inc
<?php

//fetches the current logged in users id
function fetch_current_user_id($username){
       
$username = mysql_real_escape_string($username);
 
$sql = "SELECT `user_id` FROM `users` WHERE `user_username` = '{$username}'";
 
$result = mysql_query($sql);
 
return mysql_result($result, 0);
 
}

//check if given username exsists in the database
function user_exists($user){
	$user = mysql_real_escape_string($user);
	
	$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_username` = '{$user}'");
	echo mysql_error();
	return (mysql_result($total, 0) == '1') ? true : false;

}
//check if the given username and password combinations are valid
function valid_credentials($user, $pass){
	$user = mysql_real_escape_string($user);
	$pass = mysql_real_escape_string($pass);
	
	$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_username` = '{$user}' AND `user_password` = '{$pass}'");
	echo mysql_error();
	return (mysql_result($total, 0) == '1') ? true : false;
	
}
// checks to see is user account is active
	function is_active($user){
		$user = mysql_real_escape_string($user);
		echo mysql_error();
		$sql = "SELECT
					COUNT(`activations`.`user_id`)
				FROM `users`
				INNER JOIN `activations`
				ON `users`.`user_id` = `activations`.`user_id`
				WHERE `users`.`user_username` = '{$user}'";
				
	$result = mysql_query($sql);
	echo mysql_error();
	return (mysql_result($result, 0) == '0') ? true : false;
		
}
//acctivates the account related to the given activation code
function activate_account($aid){
	$aid = mysql_real_escape_string($aid);
	
	mysql_query("DELETE FROM `activations` WHERE `activation_code` = '{$aid}'");
echo mysql_error();
}

//adds a user to the database
function add_user($user, $email, $pass, $first, $last){
	$user		= mysql_real_escape_string(htmlentities($user));
	$email		= mysql_real_escape_string($email);
	$pass		= sha1($pass);
	$first		= mysql_real_escape_string(htmlentities($first));
	$last		= mysql_real_escape_string(htmlentities($last));
	
	$charset = array_flip(array_merge(range('a', 'z'), range('A', 'Z'), range('0', '9')));
	$aid =implode('', array_rand($charset, 10)); 
	
	$body =	<<<EMAIL
	
	Thank you for signing up with knowquest. To activate your account, please click the link below
	
	http://www.jasonmassieportfolio.com/act ... ?aid={$aid}
	
EMAIL;
	
	mail($email, 'Your new account at Knowquest.com', $body, 'From: admin@knowquest.com');
	
	mysql_query("INSERT INTO `users` (`user_username`, `user_email`, `user_password`,`user_firstname`,`user_lastname`) VALUES ('{$user}', '{$email}', '{$pass}','{$first}','{$last}')");
	echo mysql_error();
	$user_id = mysql_insert_id();
	
	mysql_query("INSERT INTO `activations` (`user_id`, `activation_code`) VALUES ({$user_id}, '{$aid}')");
echo mysql_error();
}

//fetches all of the users from the 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 info for given user
function fetch_user_info($uid){
        $uid = (int)$uid;
       
        $sql = "SELECT
                   `user_id` AS `id`,
                   `user_username` AS `username`,
                   `user_email` AS `email`,
                   `user_firstname` AS `firstname`,
                   `user_lastname` AS `lastname`,
                   `user_institution` AS `institution`,
                   `user_about institution` AS `aboutinstitution`,
                   `user_professional title` AS `professionaltitle`,
                   `user_professional research` AS `professionalresearch`,
                   `user_professional website` AS `professionalwebsite`,
                   `user_personal website` AS `personalwebsite`,
                   `user_personal email` AS `personalemail`,
                   `user_professional email` AS `professionalemail`,
                   `user_about your research` AS `aboutyourresearch`,
                   `user_about yourself` AS `aboutyourself`,
                   `user_social media` AS `socialmedia`
                 FROM`users`
                 WHERE `user_id` = {$uid}";
                       
          $result = mysql_query($sql);
                 
          $info = mysql_fetch_assoc($result);
                      
          $info['avatar'] = "core.user/user_avatars/{$info['id']}.jpg";
                      
          return $info;
}
 
//updates current user portfolio info
function set_profile_info($institution, $aboutinstitution, $professionaltitle, $professionalresearch, $professionalwebsite, $personalwebsite, $personalemail, $professionalemail, $aboutyourresearch, $aboutyourself, $socialmedia, $avatar){
        $institution                    = mysql_real_escape_string(htmlentities($institution));
        $aboutinstitution               = mysql_real_escape_string(nl2br(htmlentities($aboutinstitution)));
        $professionaltitle              = mysql_real_escape_string(htmlentities($professionaltitle));
        $professionalresearch   		= mysql_real_escape_string(htmlentities($professionalresearch));
        $professionalwebsite    		= mysql_real_escape_string(htmlentities($professionalwebsite));
        $personalwebsite                = mysql_real_escape_string(htmlentities($personalwebsite));
        $personalemail                  = mysql_real_escape_string(htmlentities($personalemail));
        $professionalemail              = mysql_real_escape_string(htmlentities($professionalemail));
        $aboutyourresearch              = mysql_real_escape_string(nl2br(htmlentities($aboutyourresearch)));
        $aboutyourself                  = mysql_real_escape_string(nl2br(htmlentities($aboutyourself)));
        $socialmedia                    = mysql_real_escape_string(htmlentities($socialmedia));
 
 		 if (file_exsists($avatar)){
                $src_size = getimagesize($avatar);
               
                if ($src_size['mime'] === 'image/jpeg'){
                        $scr_img = imagecreatefromjpeg($avatar);
                       
                }else if ($src_size['mime'] === 'image/png'){
                                $scr_img = imagecreatefrompng($avatar);
                       
                }else if ($src_size['mime'] === 'image/gif'){
                                $scr_img = imagecreatefromgif($avatar);
                }else{
                        $src_img = false;
                }
                       
                if ($src_img !== false){
                                $thumb_width = 200;
                               
                                if ($scr_size[0] <= $thumb_width){
                                        $thumb = $src_img;
                                }else{
                                        $new_size[0] = $thumb_width;
                                        $new_size[1] = ($src_size[1] / $src_size[0]) * $thumb_width;
                                       
                                        $thumb = imagecreatetruecolor($new_size[0], $new_size[1]);
                                        imagecopyresampled($thumb, $src_img, 0, 0, 0, 0, $new_size[0], $src_size[0], $src_size[1]);
                        }
                        imagejpeg($thumb, "{GLOBALS['path']}/user_avatars/{$_SESSION['uid']}.jpeg");
                }
        }
       
        $sql = "UPDATE `users` SET
                    `user_institution` = '{$institution}',
                    `user_about institution` = '{$aboutinstitution}',
                    `user_professional title` = '{$professionaltitle}',
                    `user_professional research` = '{$professionalresearch}',
                    `user_professional website` = '{$professionalwebsite}',
                    `user_personal website` = '{$personalwebsite}',
                    `user_personal email` = '{$personalemail}',
                    `user_professional email` = '{$professionalemail}',
                    `user_about your research` = '{$aboutyourresearch}',
                    `user_about yourself` = '{$aboutyourself}',
                    `user_social media` = '{$socialmedia}'
                WHERE `user_id` = {$_SESSION['uid']}";
               
        mysql_query($sql);
        echo mysql_error();
               
}
 
?>
edit
<?php
error_reporting(E_ALL);

include ("core/init.inc.php");

if (isset($_POST['institution'], $_POST['aboutinstitution'], $_POST['professionaltitle'], $_POST['professionalresearch'], $_POST['professionalwebsite'], $_POST['personalwebsite'], $_POST['personalemail'], $_POST['professionalemail'], $_POST['aboutyourresearch'], $_POST['aboutyourself'], $_POST['socialmedia'])){
	$errors = array();

	if (filter_var($_POST['personalemail'], $_POST['professionalemail'], FILTER_VALIDATE_EMAIL) === false){
	$errors[] = 'The email address you entered is not valid.';

	}
	
	if (empty($_FILES['avatar']['tmp_name']) === false){
		$file_ext =	end(explode('.', $_FILES['avatar']['name']));
		
		if (in_array(strtolower($file_ext), array('jpg', 'jpeg', 'png', 'gif')) === false){
			$errors[] = 'your Picture id must be an image';
	}
	
}

	if (empty($errors)){
	set_profile_info($_POST['institution'], $_POST['aboutinstitution'], $_POST['professionaltitle'], $_POST['professionalresearch'], $_POST['professionalwebsite'], $_POST['personalwebsite'], $_POST['personalemail'], $_POST['professionalemail'], $_POST['abotyourresearch'], $_POST['aboutyourself'], $_POST['socialmedia'], (empty($_FILES['avatar']['tmp_name'])) ? false : $_FILES['avatar']['tmp_name']);
	
	}

	$user_info = array(
		'institution' 			=> htmlentities($_POST['institution']),
		'aboutinstitution' 		=> htmlentities($_POST['aboutinstitution']),
		'professionaltitle'		=> htmlentities($_POST['professionaltitle']),
		'professionalresearch'	=> htmlentities($_POST['professionalresearch']),
		'professionalwebsite' 	=> htmlentities($_POST['professionalwebsite']),
		'personalwebsite'		=> htmlentities($_POST['personalwebsite']),
		'personalemail'			=> htmlentities($_POST['personalemail']),
		'professionalemail'		=> htmlentities($_POST['professionalemail']),
		'aboutyourresearch'		=> htmlentities($_POST['aboutyourresearch']),
		'aboutyourself'			=> htmlentities($_POST['aboutyourself']),
		'socialmedia'			=> htmlentities($_POST['socialmedia'])
	);
}else{	
	$user_info = fetch_user_info($_SESSION['uid']);
}	
?><?php
	
	if (isset($errors) === false){
		echo 'Click update to Edit your portfolio.';
	}else if (empty($errors)){
		echo 'Your portfolio has been updated';
	}else{
		echo '<ul><li>', implode('</li><li>', $errors), '</li></ul>';
	}
	
	?>
<form action="" method="post" enctype="multitype/form-data">
  <table width="100%" border="0">
       <tr>
         <td width="23%"><label for='institution'>Institution:</label></td>
         <td width="77%"><input type="text" name="Institution" id="institution" value="<?php echo $user_info['institution']; ?>"></td>
       </tr>
    <tr>
        <td><label for='aboutinstituition'>About Instituition:</label></td>
        <td><textarea name="aboutinstitution" id="aboutinstitution" rows="15" cols="50"><?php echo strip_tags($user_info['aboutinstitution']); ?></textarea></td>
      </tr>
      <tr>
        <td><label for='professionaltitle'>Professional Title:</label></td>
        <td><input type="text" name="professionaltitle" id="professionaltitle" value="<?php echo $user_info['professionaltitle']; ?>"></td>
      </tr>
      <tr>
        <td><label for='professionalresearch'>Professional Research:</label></td>
        <td><input type="text" name="professionalresearch" id="professionalresearch" value="<?php echo $user_info['professionalresearch']; ?>"></td>
      </tr>
      <tr>
        <td><label for='professionalwebsite:'>Professional Website:</label></td>
        <td><input type="text" name="professionalwebsite" id="professionalwebsite" value="<?php echo $user_info['professionalwebsite']; ?>"></td>
      </tr>
       <tr>
        <td width="23%"><label for='personalwebsite'>Personal Website:</label></td>
        <td width="77%"><input type="text" name="personalwebsite" id="personalwebsite" value="<?php echo $user_info['personalwebsite']; ?>"></td>
    </tr>
    <tr>
        <td><label for='personalemail'>personal Email:</label></td>
        <td><input type="text" name="personalemail"id="personalemail"  value="<?php echo $user_info['personalemail']; ?>"></td>
      </tr>
      <tr>
        <td><label for='professionalemail'>Professional Email:</label></td>
        <td><input type="text" name="professionalemail" id="professionalemail" value="<?php echo $user_info['professionalemail']; ?>"></td>
      </tr>
      <tr>
        <td><label for='aboutyourresearch'>About your Research:</label></td>
        <td><textarea name="aboutyourresearch" id="aboutyourresearch" rows="15" cols="50"><?php echo strip_tags($user_info['aboutyourresearch']); ?></textarea></td>
      </tr>
      <tr>
        <td><label for='aboutyourself'>Autobiography:</label></td>
        <td><textarea name="aboutyourself" id="aboutyourself" rows="15" cols="50"><?php echo strip_tags($user_info['aboutyourself']); ?></textarea></td>
      </tr>
      <tr>
        <td><label for='socialmedia'>Social Networking:</label></td>
        <td><input type="text" name="socialmedia" id="socialmedia" value="<?php echo $user_info['socialmedia']; ?>"></td>
      </tr>
      <tr>
        <td><label for='avatar'>Picture Id:</label></td>
        <td><input type="file" name="avatar" id="avatar" value=""/></td>
      </tr>
  </table>
  <p><br><br>
    <input type="submit" value="Update">
  </p>
  
    </form>
index
<?php

include('core/init.inc.php');

$errors = array();

if (isset($_POST['username'], $_POST['password'])){
	if (empty ($_POST['username'])){
		$errors[] = 'The username cannot be empty!';
	}
	
	if (empty ($_POST['password'])){
		$errors[] = 'The password cannot be empty!';
	}
	
	if (valid_credentials($_POST['username'], sha1($_POST['password'])) === false){
		$errors[] = 'Username or Password incorrect!';
	}
	
	if (empty($errors) && is_active($_POST['username']) === false){
		$errors[] = 'This account has not yet been activated, Please check your email!';
	}
	
	if (empty($errors)){
	if (isset($_POST['set_cookie']) && $_POST['set_cookie'] == '1'){
		setcookie('username', $_POST['username'], time() + 604800);
		setcookie('password', sha1($_POST['password']), time() + 604800);
	}
	
	$_SESSION['username'] = htmlentities($_POST['username']);
	
	header('Location: <?php echo "member1.php?uid={uid}"; ?>');
	die();
	
	}
}

?>

<?php
	echo 'Need an account? <a href="sign up1.php">Sign Up </a>';
    if (empty($errors) === false){
	?>
    <ul>
    <?php
	foreach ($errors as $error){
		echo "<li>{$error}</li>";
		}
	?>
    </ul>
    <?php
	}
		
	?>


HAHAHAHA I FIXED THE AVATAR DEFAULT !!!! :lol: :lol: :D :D :D so now i must fix the edit page!!!!
Last edited by jaysus7 on Wed Mar 21, 2012 6:24 pm, edited 2 times in total.
Just a helpless cause!!!!
Post Reply