<?php

	require_once("constants.php");
	require_once("student.php");


	//connect to the DB server
	if(($sql = mysql_connect('localhost', DB_USER)) === FALSE)
	{
		echo "Error connecting to database.";
		exit;
	}

	//select the database
	if(mysql_select_db(DB_NAME, $sql) === FALSE)
	{
		echo "Error selecting database.";
		exit;
	}
	
	//get the record we need
	$result = mysql_query("SELECT * FROM students WHERE id=".$_GET['id']);
	$row = mysql_fetch_array($result);
?>

<html>
<head>
	<title>Edit a Student</title>
</head>
<body>


	<form action="update.php?id=<?echo $row[1];?>" method="post">
	
	<?php
		if($row[3] != "")
		{
			echo "<p>".$row[0]."'s grades are:</p>";
		
			//get his grades
			$grades = explode(",",$row[3]);
	
			//print them out nicely
			foreach($grades as $g)
			{
	?>	
				<br/>
				<input type="text" name="grades[]" size="4" value="<?echo $g?>">		
	<?
			}
		}
		else
		{
			echo $row[0]." has no grades yet. Add some.";
			echo "<br/>";
		}

		//add more fields if requrested
		if(isset($_POST['more']) && $_POST['more'] > 0)
		{
			$more = $_POST['more'];
		
			for($i=0; $i<$more; $i++)
			{
				echo "<br/><input type=\"text\" name=\"grades[]\" size=\"4\" value=\"\">";
			}
		}
	?>
		<p>Do you want to <input type="submit" value="Confirm Changes" /></p>
	</form>
	
	<form action="edit.php?id=<?echo $row[1];?>" method="post">
		<p>Or would you like to input <input type="text" name="more" size="2" value="3"> more grades?
			<input type="submit" value="Add More" /></p>
	</form>	
</body>
</html>	