<?php include "../db.php"?>
<?php
 $connection = new mysqli($dbhost,$dbusername,$dbpassword,$dbname);

if($_SERVER["REQUEST_METHOD"]=="POST" && isset($_POST["add_table_btn"])){
    $name=$_POST["add_table_name"];
    if(empty($name)){
        session_start();
        $_SESSION["table_error"]="Kindly enter table's name!!";
        header("Location: ../tables.php");
        exit();
    }
    else{
        $tableAddQuery="INSERT into tables (Tables) VALUES ('$name')";
        $connection->query($tableAddQuery);
        session_start();
        $_SESSION["table_alert"]="Table added successfully!!";
        header("Location: ../tables.php");
        exit();
    }   
}
if($_SERVER["REQUEST_METHOD"]=="POST" && isset($_POST["edit_table_btn"]))    {
    $name=$_POST["edit_table_name"];
    $id=$_POST["edit_table_id"];
    if(empty($name)){
        session_start();
        $_SESSION["table_update_error"]="Table name required to update!!";
        header("Location: ../tables.php");
        exit();
    }
    else{
        $tableEditQuery="UPDATE  tables SET Tables='$name' WHERE ID='$id' ";
        $connection->query($tableEditQuery);
        session_start();
        $_SESSION["table_update_alert"]="Table name updated successfully!!";
        header("Location: ../tables.php");
        exit(); 
    }

}
if($_POST["ID"]){
    $ID=$_POST["ID"];
    $dltTableQuery="DELETE from tables WHERE ID='$ID'";
    $connection->query($dltTableQuery);
}
?>