更新表并显示勾选的复选框

So I have the ability to submit a form which allows me to set which category my post belongs to which works great - yet if I want to edit the categories it belongs to the update is not changing the join table? I also am looking on how to show which categories are checked when you go in to update the document and pull down which categories the post belongs to.

Here is the form to update:

<?php 

require_once '../../db_con.php'; 

if(!empty($_GET['doc_id'])){
    $doc = intval($_GET['doc_id']);
try{
    $results = $dbh->prepare('SELECT * FROM doc_list WHERE doc_id = ?');
    $results->bindParam(1, $doc);
    $results->execute();

    } catch(Exception $e) {
    echo $e->getMessage();
    die();
    }

    $doc = $results->fetch(PDO::FETCH_ASSOC);    
    if($doc == FALSE){

        echo '<div class="container">';
        echo "<img src='../img/404.jpg' style='margin: 40px auto; display: block;' />";
        echo "<h1 style='margin: 40px auto; display: block; text-align: center;' />Oh Crumbs! You upset the bubba!</h1>";
        echo '<a href="userList.php"  style="margin: 40px auto; display: block; text-align: center;">Get me outta here!</a>';
        echo'</div>';
        die();
    }
}

?>


<div class="container">


<br>  
<a href="javascript:history.back(-1);" class="backLink"><i class="fa fa-angle-double-left"></i> Previous page</a> 
<br>       


<h3 class="subTitle">
    <i class="fa fa-pencil"></i></span> Edit Document
</h3>



    <?php
    if(isset($doc)){
    ?>


<form action="actions/update_doc.php" method="POST" id="rtf" name="">

    <input type="hidden" value="<?php echo $doc['doc_id'] ?>" name="doc_id" />
    <input type="text" value="<?php echo $doc['doc_title'] ?>" name="doc_title" required />
    <br />


    <?php 


    try{
        // Selecting entire row from cat_list table
        $results = $dbh->query("SELECT cat_id, cat_title FROM cat_list");

    }catch(Exception $e) {
        echo $e->getMessage();
        die();
    }
        $category = $results->fetchAll(PDO::FETCH_ASSOC);
    ?>

    <br>
    <label><input type="checkbox" name="" class="selectall"/> Select all</label>
    <div id="checkboxlist" >
    <?php
        foreach($category as $cat){ 
    ?>

    <input type="checkbox" value="<?php echo $cat["cat_id"];  ?>" name="cat_no[]" id="box1"> <?php echo $cat["cat_title"]; ?></a><br>

    <?php
    }
    ?>
    </div> 
    <br><br>


    <button class="postEditBtn" type="button" onclick="ibold()" title="Bold Text"><i class="fa fa-bold"></i></button>
    <button class="postEditBtn" type="button" onclick="iitalic()" title="Italic Text"><i class="fa fa-italic"></i></button>
    <button class="postEditBtn" type="button" onclick="iunderline()" title="Underline Text"><i class="fa fa-underline"></i></button>
    <button class="postEditBtn" type="button" onclick="ifontName()" title="Font Family"><i class="fa fa-font"></i></button>
    <button class="postEditBtn" type="button" onclick="ifontsize()" title="Font Size"><i class="fa fa-text-height"></i></button>
    <button class="postEditBtn" type="button" onclick="ifontcolor()" title="Font Colour"><i class="fa fa-eraser"></i></button>
    <button class="postEditBtn" type="button" onclick="ihiliteColor()" title="Highlight Text"><i class="fa fa-magic"></i></button>
    <button class="postEditBtn" type="button" onclick="ilink()" title="Add/Edit Link"><i class="fa fa-link"></i></button>
    <button class="postEditBtn" type="button" onclick="iunlink()" title="Remove Link"><i class="fa fa-chain-broken"></i></button>
    <button class="postEditBtn" type="button" onclick="ijustifyLeft()" title="Text align-left"><i class="fa fa-align-left"></i></button>
    <button class="postEditBtn" type="button" onclick="ijustifyCenter()" title="Text align-center"><i class="fa fa-align-center"></i></button>
    <button class="postEditBtn" type="button" onclick="ijustifyRight()" title="Text align-right"><i class="fa fa-align-right"></i></button>
    <button class="postEditBtn" type="button" onClick="iUnorderedList()" title="Unordered List"><i class="fa fa-list-ul"></i></button>
    <button class="postEditBtn" type="button" onClick="iOrderedList()" title="Ordered List"><i class="fa fa-list-ol"></i></button>
    <button class="postEditBtnUndo" type="button" onClick="iUndo()" title="Undo last change"><i class="fa fa-rotate-left"></i></button>
    <button class="postEditBtnRedo" type="button" onClick="iRedo()" title="Redo last change"><i class="fa fa-rotate-right"></i></button>



     <br><br>

    <textarea name="doc_content" id="doc_content" placeholder="Document Content" style="display: none;"></textarea>


    <iframe name="editor" id="editor" style="width:100%; height: 600px;"></iframe>   



    <br />    

    <input onclick="formsubmit()" type="submit" value="Update Document" name="submit"/>



</form>

Here is my update script:

<?php

/******************************************************************
**  ACTION SCRIPT TO UPDATE THE DOCUMENT AFTER CHANGES ARE MADE  **
******************************************************************/ 

if(isset($_POST["submit"])){
include_once'../../config.php';

try {

$dbh = new PDO("mysql:host=$hostname;dbname=dashboardr",$username,$password);

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line

$sql = "UPDATE doc_list SET doc_title = :doc_title, doc_content = :doc_content, doc_updated=CURRENT_TIMESTAMP WHERE doc_id = :doc_id";
$query = $dbh->prepare($sql);
$query->execute(array(":doc_title"=>$_POST["doc_title"],":doc_content"=>$_POST["doc_content"], ":doc_id"=> $_POST["doc_id"]));

if ($query) {
    header ('Location: ../list_doc.php?success=2');
}
/***********************************************************************
**  INSERTS THE ARRAY DEPENDING ON WHICH WAS CHECKED WITHIN THE FORM  **
***********************************************************************/


$sql = "UPDATE `cat_doc_link_table`(`link_cat_id`, `link_doc_id`) VALUES";
$values = "";
$params = [];

foreach($_POST["cat_no"] as $cat)

{

    $values.= "(?, ?), ";
    $params[] = $cat; // correct here
    $params[] = $docId;
}
    $values = substr($values, 0, -2);
    $sql.= $values;
    $query = $dbh->prepare($sql);
    $query->execute($params);       

    if ($dbh->query($sql)) {

    }else{}

    $dbh = null;
    }catch(PDOException $e)

    {
    header ('Location: ../list_doc.php?success=2');
    }

    }
?>

So I have two three tables, one called doc_list (for the posts) cat_list which handles the categories and a joint able which takes the id from both the doc_list and cat_list which marries up the two tables.

As mentioned above, in the update doc form (the first script) a way to pull in and tick the checkboxes from which category they are associated too. the main issue I have is updating the tables when you go in and want to update.