
<?php

require ('../credentials.php');
 
 
$con=mysqli_connect(HOST, USERNAME, PASSWORD, DBNAME)or die("cannot connect"); 

// array for JSON response

   
if(isset($_REQUEST['user_id']) && isset($_REQUEST['student_id']) )
{
    $user_id=$_POST['user_id'];
	 $student_id=$_POST['student_id'];
	 $student_day=$_POST['day'];
	 $student_month=$_POST['month'];

	$sql="SELECT * from student WHERE user_id='$student_id' AND day= '$student_day' AND month='$student_month'";
	 
	 $result = mysqli_query($con, $sql) or die("Error in Selecting " . mysqli_error($con));

	if (mysqli_num_rows($result)>0)
	  {
		//Student details correct
	  
	  $sql="SELECT * FROM parent_children WHERE parent_id='$user_id' AND student_id='$student_id'";
	$result = mysqli_query($con, $sql) or die("Error in Selecting " . mysqli_error($con));

	if (mysqli_num_rows($result)>0)
	  {
		  //Student Has been added already
		$response["error"] = TRUE;
		$response["error_msg"] = "Ward has already been added.";
		echo json_encode($response);

	  }else{
		
		$sql="INSERT INTO parent_children(parent_id,student_id) VALUES ('$user_id','$student_id')";
		$result = mysqli_query($con, $sql) or die("Error in Selecting " . mysqli_error($con));
		
		if(!$result){
		// user failed to store
            $response["error"] = TRUE;
            $response["error_msg"] = "Error occured !";
            echo json_encode($response);
		  }else{
			//Ward successfully added  
			$response["error"] = FALSE;
            $response["success_msg"] = "Ward successfully added.!";
            echo json_encode($response);
		    }
		  
		  }
	
	  
	  }else{
		// No Matching Student Found
		$response["error"] = TRUE;
		$response["error_msg"] = "Incorrect Ward details Provided!";
        echo json_encode($response);

	  }
    

   
}
mysqli_close($con);


?>
