<?php
include($_SERVER['DOCUMENT_ROOT'] . "/Authorization/script/php/permission/check_right.php");
include('../permission/connect_sql.php');
if (isset($_POST['UserID']) && $_POST['GroupID']) {
    $UserID = $_POST['UserID'];
    $GroupID = $_POST['GroupID'];
    $sql = "BEGIN
                IF NOT EXISTS (SELECT [UserID],[GroupID] FROM [UserGroup] 
                    WHERE [UserID] = ? AND [GroupID] = ?)
                BEGIN
                    INSERT INTO [UserGroup] ([UserID],[GroupID])
                    VALUES  (?, ?)
                END
            END";
    $stmt = sqlsrv_query($conn, $sql, array($UserID, $GroupID, $UserID, $GroupID));
    if ($stmt === false) {
        if (($errors = sqlsrv_errors()) != null) {
            foreach ($errors as $error) {
                echo "SQLSTATE: " . $error['SQLSTATE'] . "<br />";
                echo "code: " . $error['code'] . "<br />";
                echo "message: " . $error['message'] . "<br />";
            }
        }
    }else{
        echo('success');
    }
}
