<?php
    include 'needs_login.php';
    include 'db_conn.php';

    function delete_label_links($user_id)
    {
        global $db;
        $stmt = $db->prepare("DELETE FROM User_labels WHERE user_id=:uid");
        $stmt->execute(array("uid" => $user_id));
    }

    function insert_label_link($user_id, $label_id)
    {
        global $db;
        $stmt = $db->prepare("INSERT INTO User_labels (user_id, label_id) VALUES (:uid, :lid)");
        $stmt->execute(array("uid" => $user_id, "lid" => $label_id));
    }

    delete_label_links($_SESSION['user_id']);

    if (isset($_POST['labels']))
    {
        foreach ($_POST['labels'] as $value)
        {
            insert_label_link($_SESSION['user_id'], $value);
        }
    }
?>
