<?php include '../db.php' ?>
<?php
 $connection = new mysqli($dbhost,$dbusername,$dbpassword,$dbname); 
 
if($_SERVER["REQUEST_METHOD"]=="POST" && isset($_POST["catogery_submission"])){
$name= $_POST["catogery_name"];
if(empty($name)){
  session_start();
  $_SESSION["caterror"]="Kindly Insert All Fields";
  header("Location: ../categories.php");
  exit();
}
else{
  $addCatQuery="INSERT into category (Name) VALUES ('$name')";
  $connection->query($addCatQuery);

  session_start();
  $_SESSION["catalert"] = "Data Insert Sucessfully!";
  header("Location: ../categories.php");
  exit();
}
};
if($_SERVER["REQUEST_METHOD"]=="POST" && isset($_POST["category_edit"])){

  $catId=$_POST["category_id"];
  $catname=$_POST["category_edit_name"];
  if(empty($catname)){
     session_start();
  $_SESSION["catupdateerror"]="Category name is required!!";
  header("Location:../categories.php");
  exit();
  }
  else{

    $catEditquery="UPDATE category SET Name='$catname' WHERE ID='$catId' ";
    $connection->query($catEditquery);
  session_start();
  $_SESSION["catupdatesuccess"]="Category updated successfully!!";
  header("Location:../categories.php");
  exit();
}
};

if (isset($_POST['id'])) {
    $id = $_POST['id'];
    $query = "DELETE FROM category WHERE id='$id'";
    if ($connection->query($query)) {
        echo "Record deleted successfully!";
    } else {
        echo "Error deleting record.";
    }
}



?>