<?php
include("../../include/db.php");
/*********************************Check item********************************************************/

function check_place_namess($from_place,$to_place)
{
	$Isql=mysql_query("select * from place_creation where (from_place='$from_place' and to_place='$to_place') ");
	$rscount=mysql_num_rows($Isql);
	return $rscount;
}
/*********************************Check item for edit********************************************************/

function check_place_edit($from_place,$to_place)
{
	 $sql_query="select * from place_creation where from_place='$from_place' and to_place='$to_place'";
$rs=mysql_query($sql_query);
 $rscount1=mysql_num_rows($rs);
 if($rscount1!=0)
 {
 while($rsdata=mysql_fetch_object($rs))
 {
 $id=$rsdata->id;
 } 
 }
 return $id;
 }
 
 /********************************** ADD  QUERY ***************************************/
if(($_POST['action']=="ADD")) 
{
$from_place=ucfirst($_POST['from_place']);
$to_place=ucfirst($_POST['to_place']);
$location=$from_place."-".$to_place;
$kms=$_POST['kms'];
$create_date=date('Y-m-d');
$acadamic_yr=explode("-",$create_date);
$acdmc_yr_res=$acadamic_yr[0];
$check_place_name=check_place_namess($from_place,$to_place);
if($check_place_name==0)
{
$Insql=mysql_query("insert into place_creation(from_place,to_place,location,kms,acad_yr,cr_date)values('$from_place','$to_place','$location','$kms','$acdmc_yr_res','$create_date')");
echo "Added Successfully!!";
}
else
{
echo "Already Exist!!";
}
}
/********************************** UPDATE QUERY ***************************************/
if(($_POST['action']=="EDIT"))
{
$edit_id = $_POST['edit_id']; 
$from_place=ucfirst($_POST['from_place']);
$to_place=ucfirst($_POST['to_place']);
$location=$from_place."-".$to_place;
$kms=$_POST['kms'];
$create_date=date('Y-m-d');
$acadamic_yr=explode("-",$create_date);
$acdmc_yr_res=$acadamic_yr[0];
 $check_edit_item=check_place_edit($from_place,$to_place);

if(($check_edit_item==$_POST['edit_id'])||($check_edit_item==''))
{
$Insql=mysql_query("update place_creation set from_place='$from_place',to_place='$to_place',location='$location',kms='$kms',acad_yr='$acdmc_yr_res',cr_date='$create_date' where  id='$_POST[edit_id]'");
echo "Updated Successfully!!";
}
else
{
	echo "Already Exist";
}
}
/********************************************DELETE*****************************************************************/
if($_POST['action']=="delete_place")
{ 		 
		$delete2="delete from place_creation where id='$_POST[id]'";
		mysql_query($delete2);
		echo "<script>alert('Deleted Succesfully')</script>";
		include("place_entry_list.php");
}


?>