<?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;
	}

	//find the student record we want to update
	$id = $_GET['id'];
	
	//format the grades as a string
	$grades = implode(",",$_POST['grades']);

	//create a new student object
	$student = new Student("dummy", $id, "11111111");
	
	//add the grades to get a gpa
	foreach($_POST['grades'] as $g)
	{
		$student->AddGrade($g);
	}
	
	//update the database
	$query = "UPDATE students SET Grades='".$grades."', GPA=".$student->GetGPA()." WHERE id='".$id."'";
	$result = mysql_query($query);
	
?>

<a href="list.php">Back to my students</a>