<?php

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

?>
<html>
<head>
<title>Add an Student</title>
</head>
<body>
<?php
	
//bit of checking
if (empty($_POST['name']) || empty($_POST['id']) || empty($_POST['phone'])) {
	//Not all fields have been specified!
	echo "<h1>Error</h1>";
	echo "<p>You need to specify all the required fields!</p>";
	echo "<a href=\"index.php\">Go Back</a>";
	//We need to print out the footer too, because we're exiting out of the script
	echo "</body></html>";
	exit;
}

//Read in variables from $_POST
$name = $_POST['name'];
$id = $_POST['id'];
$phone = $_POST['phone'];
?>

<h1>Add a Student</h1>

<?
//create a student object
$student = new Student($name, $id, $phone);

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

$newstring = mysql_real_escape_string($string);

//select the database
if(mysql_select_db(DB_NAME, $sql) === FALSE)
{
	echo "Error selecting database.";
	exit;
}

//make the string
$query = "INSERT INTO students (Name, ID, Phone, GPA) VALUES('".$student->GetName()."', '".$student->GetID()."', '".$student->GetPhone()."', '".$student->GetGPA()."')";

//execute the query
if(($result = mysql_query($query)) === TRUE)
{
	echo "<p>Your student ".$student->GetName()." has been successfully added.</p>";
	echo "<a href=\"list.php\">Go to student list!</a>";
}
else
{
	echo "<p>ERROR: Your student ".$student->GetName()." has not been added.</p>";
	echo "<a href=\"list.php\">Go to student list!</a>";
}
?>
</body>
</html>
