<?php
//include "application/scripts/connect.php";
if(isset($_POST['student_address'])){
define('BASEPATH', "/smartsms/");
define('ENVIRONMENT', "production");
include "../config/database.php";

$hostname = $db['default']['hostname'];
$username = $db['default']['username'];
$password = $db['default']['password'];
$database = $db['default']['database'];
$dbdriver = $db['default']['dbdriver'];


// Create connection
$conn_db = mysqli_connect($hostname, $username, $password, $database);
// Check connection
if (!$conn_db) {
    die("Connection failed: " . mysqli_connect_error());
}
	//student_session,current_term,this_student_id,type,this_comment
	$class_data = $_POST['class_data'];
	$class_data_array = explode('_', $class_data);
	$class_data_class_id = $class_data_array[0];
	$class_data_section_id = $class_data_array[1];
	$head_address = $_POST['head_address'];
	$teacher_address = $_POST['teacher_address'];
	$student_address = $_POST['student_address'];
			
		$sql2 = "SELECT * from `exam_result_label` WHERE class_id='$class_data_class_id' AND section_id='$class_data_section_id' ORDER BY `id` DESC LIMIT 1;";
	$query2 = mysqli_query($conn_db, $sql2);
		if(mysqli_num_rows($query2) == 0){
		$sql3 = "INSERT INTO `exam_result_label` (`class_id`, `section_id`, `head_address`, `teacher_address`, `student_address`) 
		VALUES ('$class_data_class_id', '$class_data_section_id', '$head_address', '$teacher_address', '$student_address');";
		$query3 = mysqli_query($conn_db, $sql3);
		//return "";
		}
		else {
	$sql = "UPDATE `exam_result_label` SET
			`head_address` = '$head_address',
			`teacher_address` = '$teacher_address',
			`student_address` = '$student_address'
			WHERE `class_id` = '$class_data_class_id' AND `section_id` = '$class_data_section_id';";
	
		//echo $sql;
		
			if($query = mysqli_query($conn_db, $sql)){	
			echo "
			<script>
			alert('Class Label Updated');
			</script>";
			}
			else{
				echo "<script>alert('Class Label failed');</script>";
			}
			

		}
	
}




?>  