<?php

date_default_timezone_set("Asia/Jerusalem");

require __DIR__ . '/vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . "/../");
$dotenv->load();

$host_ip = $_SERVER['HOST_IP'];
$db_user = $_SERVER['DB_USER'];
$db_pass = $_SERVER['DB_PASS'];
$db_name = $_SERVER['DB_NAME'];

$con = mysqli_connect($host_ip, $db_user, $db_pass, $db_name);

if (mysqli_connect_errno()) {
  echo "Failed to connect to SQL: " . mysqli_connect_error();
}

mysqli_query($con, "
CREATE TABLE IF NOT EXISTS `$table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `phone` varchar(45) NOT NULL,
  `mac` char(17) NOT NULL CHECK (mac <> ''),
  `apmac` char(17) NOT NULL CHECK (apmac <> ''),
  `site` varchar(45) NOT NULL,
  `last_updated` varchar(45) NOT NULL,
  PRIMARY KEY (`id`)
)");

$phone = $_SESSION["phone"];
$mac = $_SESSION["mac"];
$apmac = $_SESSION["apmac"];
$site = $_SESSION["site"];
$last_updated = date("Y-m-d H:i:s");

mysqli_set_charset($con, "utf8");

mysqli_query($con, "INSERT INTO `$table` (phone, mac, apmac, site, last_updated) VALUES ('$phone', '$mac', '$apmac', '$site', '$last_updated')");