<?php 
include ('includes/db1config.php');
include ('../includes/mysql_connect.php');

if(!empty($_POST)){

	$checkalias  = 'SELECT alias from document_alias where filename ="'.$_POST['file_name'].'"';  
	$rt          = mysql_query($checkalias); // query executed 
    echo mysql_error();      // if any error is there that will be printed to the screen
    $nt          = mysql_fetch_array($rt);
    if( isset($nt['alias']) && $nt['alias']!='' ){
		$query 	= 'UPDATE document_alias SET alias="'.mysql_escape_string($_POST['alias']).'" WHERE filename="'.$_POST['file_name'].'"';
		mysql_query($query) or die(mysql_error());    
		echo json_encode(
			array(
			 	'message' => 'Successfully updated alias name.',
			 	'alias'   => $_POST['alias'],
			 	'status'  => 1,
			)
		);	
    } else {

		$query 	= "INSERT INTO document_alias(alias,filename) VALUES ('".mysql_escape_string($_POST['alias'])."','".mysql_escape_string($_POST['file_name'])."')";
		mysql_query($query) or die(mysql_error());


		$checkalias  = 'SELECT alias from document_alias where filename ="'.$_POST['file_name'].'"';  
		$rt          = mysql_query($checkalias); // query executed 
	    echo mysql_error();      // if any error is there that will be printed to the screen
	    $nt          = mysql_fetch_array($rt);
	    
	    echo json_encode(
			array(
			 	'message' => 'Successfully updated alias name.',
			 	'alias'   => $nt['alias'],
			 	'status'  => 1,
			)
		);
    }
} else {
	echo json_encode(
		array(
		 	'message' => 'Something went wrong.',
		 	'status'  => 0,
		)
	);

}
