<?php
	require_once '../../autoload.php';
	
	$id		 		= -1;
	$title 			= "";
	$description	= "";
	$sex_id			= -1;
	$age_id			= -1;
		
	
	if ( isset ( $_REQUEST['id'] ) 				)	$id		 		= $_REQUEST['id'];
	if ( isset ( $_REQUEST['title'] )			)	$title		 	= $_REQUEST['title'];
	if ( isset ( $_REQUEST['description'] )		)	$description	= $_REQUEST['description'];
	if ( isset ( $_REQUEST['sex_id'] )			)	$sex_id			= $_REQUEST['sex_id'];
	if ( isset ( $_REQUEST['age_id'] )			)	$age_id			= $_REQUEST['age_id'];
			
	$conn = get_dbconnection();
	
	$response = -1;
	
	if ( $conn )
	{
		$sql = '';

		if ( $id == -1 )
			$sql = 'insert into examwarning(title,sex_id, age_id) values (\''.pg_escape_string ($conn,$title).'\', -1, -1) returning id;';
		else
			$sql = 'update examwarning set title=\''.pg_escape_string ($conn,$title).'\', description=\''.pg_escape_string ($conn,$description).'\', sex_id='.$sex_id.', age_id='.$age_id.' where id='.$id.'; commit;';
		
		$result = pg_query ( $conn, $sql );
		
		if ( $result )
		{
			if ( $id == -1 )
			{
				if ( $row = pg_fetch_row($result) )
					$response = $row [0];
			}
			else
				$response = $id;
		}
	}
	
	header ('Content-type: text/xml' );
	
	echo $response;
?>