Building your own Myspace.com with PHP part IV: Presentation

Posted by Stefan on July 31st, 2007 at 08:59pm

In the previous tutorial we learned how to register a new user and how to create a login system with php sessions. With this done we can now move on to the users personal presentation page. So in this tutorial we will first go over how to display a presentation and then how to let the user edit his own page.

Pretty much everything of this will go in a file I have called members.php, let’s go over the file from top to bottom.

<?php
// members.php
if(isset($_GET["id"]))
{
	// Check if user exists in the database
	$member = $_GET["id"];
	require_once("classes/DbConnector.php");
	$db = new DbConnector();
	$db->connect();
	$query = "SELECT * FROM members WHERE username='$member'";
	$result = $db->query($query);
	$exists = mysql_num_rows($result);  // Does the row exists?
 
	if($exists !="0"){ // Presentation exists so display it
		$rows = $db->fetchArray($result); // Get the profile from database
		echo $rows["presentation"]."<br/><br/>"; 
 
 
		//TODO: Display guestbook here
 
	}	
	else 
	{
		echo "That member does not exist";
	}
}
?>

This code is fairly simple. Basically what it does is that it gets the variable id that is passed to the file as an argument (remember how the login function forwarded to this page in the previous tutorial?) and then checks to see if there is a user by that name in the database. If there is a match that users presentation is displayed on the page.

Next on the list of features to add is the possibility to edit the presentation.

if(isset($_GET["edit"])) // Edit profile
{	
	// First lets make sure the user is logged in 	
	if(session_is_registered("username") && session_is_registered ("password") && $_SESSION["username"] == $_GET["edit"])
	{
		if(isset($_GET["update"]))
		{
			require_once("classes/DbConnector.php");
			$member = $_GET["edit"];
			$db = new DbConnector();
			$db->connect();
			$presentation = $_POST["presentation"];
			$query = "UPDATE members SET presentation='$presentation' WHERE username='$member'";
			$result = $db->query($query);
			echo "Profile updated!";		
		}
		else 
		{	// Display edit box
			require_once("classes/DbConnector.php");
			$member = $_GET["edit"];
			$db = new DbConnector();
			$db->connect();
			$query = "SELECT * FROM members WHERE username='$member'";
			$result = $db->query($query);
			$rows = $db->fetchArray($result);
			echo "<center><br/><b>Edit your profile</b><br/>
				<form action=\"member.php?edit=".$_GET["edit"]."&update\" method='POST'>
					<textarea name='presentation' rows='10' cols='80' align='left'>"
					.$rows["presentation"].
					"</textarea><br/>
					<input type='submit' value='Update' name='submit'>
				</form>
				</center>
			";
 
		}
	}
}

If we now call members.php?edit=username an edit box will appear that allows the user to edit his presentation. When the edit form is submitted we update the database with the new presentation.

Next part: Friends and guestbook

Under MySQL+ PHP+ Tutorials

1 Comment for Building your own Myspace.com with PHP part IV: Presentation

  • 1. Rob Foley  |  June 29th, 2008 at 8:01 am

    Does anyone if the the edit profile, guestbook, list of friends and search is all in members.php. i cant tell
    thanks

Leave a Comment for Building your own Myspace.com with PHP part IV: Presentation

hidden

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent Blog Posts

Categories

Posts by Month

Blogroll