<?php
ob_start();
session_start();
/*
echo "isset: ", isset($_COOKIE["PHPSESSID"]), "<p>";
echo "get ID: ", $_GET['PHPSESSID'], "<br>";
echo SID, "<br>";
*/
?>

<html>
 <head>
  <title></title>
 </head>
 <body onload="document.Form2.reset(); document.Form2.First_Name.focus();">
  <center>
<?php
/*
Here we use the username variable stored in $_SESSIONS 
instead of suing hidden fields or cookies.
*/

function display_form($username,$first_name,$last_name,$email,$phone,$address,$major,$graduation_year,$date)
{
	echo <<< Begin
		<H2 align="center">Your Information</H2>
        	</center>
        	<p></p>
        	<CENTER>
        	<form action="{$_SERVER['PHP_SELF']}" name="Form2" method="POST">

		<table bgcolor="lightgrey" border="1" bordercolor="gray">
            
		<TR>
        	<TD><B>Your First Name:</B></TD>
        	<TD><INPUT NAME="First_Name" SIZE="50" value="$first_name"></TD>
        	</TR>
		
		<TR>
        	<TD><B>Your Last Name:</B></TD>
        	<TD><INPUT NAME="Last_Name" SIZE="50" value="$last_name"></TD>
        	</TR>

		<TR>
        	<TD><B>Your Email:</B></TD>
        	<TD><INPUT NAME="Email" SIZE="50" value="$email"></TD>
        	</TR>

		<TR>
        	<TD><B>Phone:</B></TD>
        	<TD><INPUT NAME="Phone" size="50" value="$phone"></TD>
       	 </TR>
	
	 	<TR>
        	<TD><B>Address:</B></TD>
        	<TD><INPUT NAME="Address" size="50" value="$address"></TD>
        	</TR>

        	<TR>
       	 <TD><B>Major:</B></TD>
        	<TD><INPUT NAME="Major" size="50" value="$major"></TD>
       	 </TR>

        	<TR>
       	 <TD><B>Graduation Year:</B></TD>
        	<TD><INPUT NAME="Graduation_Year" size="50" value="$graduation_year"></TD>
       	 </TR>
			
		<TR>
       	 <TD colspan="2" align="center"><B>Last updated:</B> $date.</TD>
       	 </TR>

		
       	<input name='submitted' value='TRUE' type='hidden'>
	<!-- The following hidden fields are intended for this page not the next one-->
	
		<input name='Username' value='$username' type='hidden'>

       	 </TABLE>

        	<TABLE>
		<tr><td colspan="2" bgcolor="white" height="10"></td></tr>
		<tr><td colspan="2" align="center" BGCOLOR="lightgrey">
		If you update the information, press the submit button.
		</td></tr>
        	<TR>
        	<TD align="center" BGCOLOR="lightgrey"><INPUT TYPE="submit" VALUE="SUBMIT">
       	 </TD>
       	 <TD align="center" BGCOLOR="lightgrey">
       	 <INPUT TYPE="reset" VALUE="RESET">
        	</TD>
        	</TR>
       	 </table>

       	 </form></center>
Begin;
}

function read_from_db($username,&$first_name,&$last_name,&$email,&$phone,&$address,&$major,&$graduation_year,&$date)
{
	@$d = mysql_connect('localhost','abu-jeib','iyad911');
	if (!$d)
	{
		echo "Sorry can't process your information at this time (can't connect).<BR>";
		return FALSE;
	}

	@$s = mysql_select_db('alumni');
	if (!$s)
	{
		echo "Sorry can't process your information at this time (can't use database).<BR>";
		return FALSE;
	}
	
	$q = "select first_name, last_name, email, address, phone, major, graduation_year, date from users 
		where username='$username'";
	@$r = mysql_query($q);
	if (!$r)
	{
		echo "Sorry can't process your information at this time (can't process query).<BR>";
		@mysql_close($d);
		return FALSE;
	}
	else
	{
		while ($row = mysql_fetch_row($r))
		{
			$first_name = $row[0];
			$last_name = $row[1];
			$email = $row[2];
			$address = $row[3];
			$phone = $row[4];
			$major = $row[5];
			$graduation_year = $row[6];
			$date = $row[7];
		}
	}
	@mysql_free_result($r);
	@mysql_close($d); 
	return TRUE;
}

function write_to_db($username,$first_name,$last_name,$email,$phone,$address,$major,$graduation_year)
{
	$s = array($username,$first_name,$last_name,$email,$phone,$address,$major,$graduation_year);
	foreach ($s as $v)
		if (empty($v))
		{
			echo "You have to fill out all the fields.<br>";
			return FALSE;
		}
	@$d = mysql_connect('localhost','abu-jeib','iyad911');
	if (!$d)
	{
		echo "Sorry can't process your information at this time (can't connect).<BR>";
		return FALSE;
	}

	@$s = mysql_select_db('alumni');
	if (!$s)
	{
		echo "Sorry can't process your information at this time (can't use database).<BR>";
		return FALSE;
	}

	$q = "update users set first_name='$first_name',last_name='$last_name', date = now(), 
		email='$email',phone='$phone',address='$address',major='$major',graduation_year='$graduation_year' 
		where username='$username'";
	@$r = mysql_query($q);
	if (!$r)
	{
		echo "Sorry can't process your information at this time (can't process query).<BR>";
		@mysql_close($d);
		return FALSE;
	}

	// mysql_free_result($r); // NO, don't here.
	@mysql_close($d);
	return TRUE;
}

if (isset($_SESSION['username'])) 
{
	$username = $_SESSION['username'];
	if (!empty($username))
	{
		$flag = read_from_db($username,$first_name,$last_name,$email,$phone,$address,$major,$graduation_year,$date);
		if (!$flag)
			exit("<p>We can't process your request at this time.</p>");
		if (!isset($_POST['submitted']))
			display_form($username,$first_name,$last_name,$email,$phone,$address,$major,$graduation_year,$date);
		else
		{	
			$username = trim($_POST['Username']);
			$first_name = trim($_POST['First_Name']);
			$last_name = trim($_POST['Last_Name']);
			$email = trim($_POST['Email']);
			$phone = trim($_POST['Phone']);
			$address = trim($_POST['Address']);
			$major = trim($_POST['Major']);
			$graduation_year = trim($_POST['Graduation_Year']);

			$flag = write_to_db($username,$first_name,$last_name,$email,$phone,$address,$major,$graduation_year);
			if (!$flag)
				exit("<p>We can't process your request at this time.</p>");
			echo "<h2 align='center'>Your information has been updated. Thank you.</h2>";
			echo "<center><a href='members_alumni_sessions.php'>Back to the main page</a></center></br>";				
		} // else for if form submitted
	}
	else
	{
		echo "<p>You are not logged in.</p>\n";
		echo "<a href='current_alumni_sessions.php'>Member Login</a><br>\n";	
		echo "<a href='new_alumni.php'>New Member</a>\n";
	}
}
else
{
	echo "<p>You are not logged in.</p>\n";
	echo "<p><a href='current_alumni_sessions.php'>Member Login</a></p><br />\n";	
	echo "<p><a href='new_alumni.php'>New Member</a></p>\n";
}
ob_end_flush();
?>

</center></body></html>
