if (isset($_POST['tags'])) {
     $tags = $_POST['tags'];
 }
 //DELETE TAGS from Cookbook
 $sql = "DELETE FROM Tag WHERE type='COOKBOOK' AND type_id ='{$cookbook_id}'";
 if ($link->query($sql) != true) {
     header('Location: fail.php');
 }
 //DELETE friends of cookbook
 $sql = "DELETE FROM Friends WHERE type='COOKBOOK' AND type_id ='{$cookbook_id}'";
 if ($link->query($sql) != true) {
     header('Location: fail.php');
 }
 //If its friendly, add emails
 if ($privacy == "friendly") {
     storeFriends($allemails, $cookbook_id, $link);
     //$check = $allemails;
 }
 //Update cookbook name and privacy
 $sql = "UPDATE Cookbook SET cb_title = '{$cookbookname}', visibility = '{$privacy}' WHERE cookbook_id = '{$cookbook_id}'";
 if ($link->query($sql) != true) {
     $error = "ERROR: Could not able to execute {$sql}. " . $link->connect_error;
 }
 //store array of tags in database
 if (isset($tags)) {
     storeTags($tags, $cookbook_id, $link);
 }
 redirect($cookbook_id);
 //redirect back to view cookbook
 mysqli_close($link);
 //close connection
    $sql = "DELETE FROM Friends WHERE type='COOKBOOK' AND type_id ='{$cookbook_id}'";
    if ($link->query($sql) != true) {
        header('Location: fail.php');
    }
    //Update cookbook name and privacy
    $sql = "UPDATE Cookbook SET cb_title = '{$cookbookname}', visibility = '{$privacy}' WHERE cookbook_id = '{$cookbook_id}'";
    if ($link->query($sql) != true) {
        $error = "ERROR: Could not able to execute {$sql}. " . $link->connect_error;
    }
    //store array of tags in database
    if (isset($tags)) {
        storeTags($tags, $cb_id, $link);
    }
    //If its friendly, add emails
    if ($privacy == "friendly") {
        $count = storeFriends($email, $cb_id, $link);
        $error = $email;
    }
    redirect();
    mysqli_close($link);
    //close connection
}
function storeTags($tag, $cookbook_id, $link)
{
    $size = count($tag);
    for ($i = 0; $i < $size; $i++) {
        $current = $tag[$i];
        $sql = "INSERT INTO  Tag (name, type, type_id)\n                VALUES ('{$current}', 'COOKBOOK', '{$cookbook_id}')";
        if ($link->query($sql) != true) {
            $error = "ERROR: Could not execute {$sql}. " . $link->connect_error;
        }
    if (isset($_POST['email'])) {
        $allemails = $_POST['email'];
    }
    if (isset($_POST['tags'])) {
        $tags = $_POST['tags'];
    }
    //ADD NEW COOKBOOK TO Cookbook Table
    $sql = "INSERT INTO {$tbl_name} (cb_title, visibility)\n                VALUES ('{$cookbookname}', '{$privacy}')";
    if ($link->query($sql) != true) {
        $error = "ERROR: Could not able to execute {$sql}. " . $link->connect_error;
    }
    //get new cookbook id
    $cb_id = mysqli_insert_id($link);
    //If its friendly, add emails
    if ($privacy == 'friendly') {
        storeFriends($allemails, $cb_id, $link);
    }
    //Add to Cookbook list of user
    $sql = "INSERT INTO Cookbook_list (user_id, cookbook_id)\n            VALUES ('{$user_id}', '{$cb_id}')";
    if ($link->query($sql) != true) {
        $error = "ERROR: Could not able to execute {$sql}. " . $link->connect_error;
    }
    //store array of tags in database
    if (isset($tags)) {
        storeTags($tags, $cb_id, $link);
    }
    redirect($cb_id);
    mysqli_close($link);
    //close connection
}
//Store tags in database