<?php session_start();
include("conn.php");
if($_POST['action'])
{
  $action=$_POST['action'];
  //Add Data Start
  if($action=="Add")
  {
    if($_POST['state'] && $_POST['district'] && $_POST['area'] && $_POST['category']  && $_POST['des'] && $_POST['title'] && $_POST['e_date'])

    {
       
      $v_code=$_POST['v_code'];
        $state=$_POST['state'];
        $district=$_POST['district'];
        $area=$_POST['area'];
        $category=$_POST['category'];
        $title = $_POST['title'];
        $des = $_POST['des'] ;
        $event_url = $_SESSION['event_url'];
        $e_date = $_POST['e_date'];

        $sql="insert into tbl_post(v_code,state,district,area,category,event_url,title,des,e_date)values('$v_code','$state','$district','$area','$category','$event_url','$title','$des','$e_date')";
        if(mysqli_query($conn,$sql))
        {
          echo "Record added successfully.";
        }
        else
        {
          echo "Record not added. Error:".mysqli_error($conn);
        }

    }
    else
    {
      echo "Please complete all required fields.";
    }
  }
  //Add Data End  
  //Update Data Start
  else if($action=="Edit")
  {
    if($_POST['state'] && $_POST['district'])
    {
        $id=$_POST['id'];
        $v_code=$_POST['v_code'];
        $state=$_POST['state'];
        $district=$_POST['district'];
        $area=$_POST['area'];
        $category=$_POST['category'];
        $event_url = $_SESSION['event_url'];
        $des = $_POST['des'] ;
        $title = $_POST['title'];
        $e_date = $_POST['e_date'];
       
        $sql="update tbl_post set v_code='$v_code',state='$state',district='$district',area='$area',category='$category',event_url='$event_url',title='$title',des='$des',e_date='$e_date', updated_at=CURRENT_TIMESTAMP() where id='$id'";   
       
        if(mysqli_query($conn,$sql))
        {
          echo "Record updated successfully.";
        }
        else
        {
          echo "Record not updated. Error:".mysqli_error($conn);
        }

    }
        else
        {
          echo "Please complete all required fields.";
        }
  }
  //Update Data End
  //Delete Data Start
  else if($action=="Delete")
  {
    $id=$_POST['id'];

    $sql="update tbl_post set deleted_at=CURRENT_TIMESTAMP() where id='$id'";
    if(mysqli_query($conn,$sql))
    {
      echo "Record deleted successfully.";
    }
    else
    {
      echo "Record not deleted. Error:".mysqli_error($conn);
    }
  }
  //Delete Data End
}
else
{
  echo "Invalid action specified.";
}


  mysqli_close($conn);

