<?php
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo 'Access Denied';
    exit();
}

include 'config.php';

$check = $conn->query("SHOW TABLES LIKE 'match'");
if ($check->num_rows == 0) {
    $conn->query("CREATE TABLE `match` (
        id INT AUTO_INCREMENT PRIMARY KEY,
        `Banner` TEXT,
        `Logo` TEXT,
        `Tittle` TEXT,
        `Time` TEXT,
        `Total_Prize` TEXT,
        `Per_Kill` TEXT,
        `Entry_Fee` TEXT,
        `Type` TEXT,
        `Version` TEXT,
        `Map` TEXT,
        `Player` TEXT,
        `Prize_Pool` TEXT,
        `Live_Url` TEXT,
        `Button` TEXT,
        `Joined` TEXT,
        `Key` TEXT,
        `Participate` TEXT,
        `Room_Id` TEXT,
        `Tag` TEXT,
        `Category` TEXT,
        `ss` TEXT,
        `Winner` TEXT,
        `n1` TEXT,
        `n0` TEXT,
        `Name_0` TEXT,
        `Name_1` TEXT,
        `Joiners` TEXT
    )");
}

$Banner = $_POST['Banner'];
$Logo = $_POST['Logo'];
$Tittle = $_POST['Tittle'];
$Time = $_POST['Time'];
$Total_Prize = $_POST['Total_Prize'];
$Per_Kill = $_POST['Per_Kill'];
$Entry_Fee = $_POST['Entry_Fee'];
$Type = $_POST['Type'];
$Version = $_POST['Version'];
$Map = $_POST['Map'];
$Player = $_POST['Player'];
$Prize_Pool = $_POST['Prize_Pool'];
$Live_Url = $_POST['Live_Url'];
$Button = $_POST['Button'];
$Joined = $_POST['Joined'];
$Key = $_POST['Key'];
$Participate = $_POST['Participate'];
$Room_Id = $_POST['Room_Id'];
$Tag = $_POST['Tag'];
$Category = $_POST['Category'];
$ss = $_POST['ss'];
$Winner = $_POST['Winner'];
$n1 = $_POST['1'];
$n0 = $_POST['0'];
$Name_0 = $_POST['Name_0'];
$Name_1 = $_POST['Name_1'];
$Joiners = $_POST['Joiners'];

$sql = "INSERT INTO `match` (`Banner`, `Logo`, `Tittle`, `Time`, `Total_Prize`, `Per_Kill`, `Entry_Fee`, `Type`, `Version`, `Map`, `Player`, `Prize_Pool`, `Live_Url`, `Button`, `Joined`, `Key`, `Participate`, `Room_Id`, `Tag`, `Category`, `ss`, `Winner`, `n1`, `n0`, `Name_0`, `Name_1`, `Joiners`) VALUES ('$Banner', '$Logo', '$Tittle', '$Time', '$Total_Prize', '$Per_Kill', '$Entry_Fee', '$Type', '$Version', '$Map', '$Player', '$Prize_Pool', '$Live_Url', '$Button', '$Joined', '$Key', '$Participate', '$Room_Id', '$Tag', '$Category', '$ss', '$Winner', '$n1', '$n0', '$Name_0', '$Name_1', '$Joiners')";

if ($conn->query($sql) === TRUE) {
    echo "Match Add Successfully";

    $catId = $_POST['id'];
    $result = $conn->query("SELECT Length, Total FROM category WHERE id='$catId'");
    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $newLength = $row['Length'] + 1;
        $newTotal = $row['Total'] + 1;
        $conn->query("UPDATE category SET Length='$newLength', Total='$newTotal' WHERE id='$catId'");
    }

} else {
    echo "Error";
}
?>