如何使用数据库中的预选值创建复选框?

I am trying to create checkbox with preselected values which comes from my database (Genres). For example, if this movie has 3 different genres: comedy, romance and action (in the database), these genres will be preselected (have default value) checkboxes. The problem is that i need all the checkboxes (genres) which I get from the following SQL-query:
$sql = 'SELECT Movie.* FROM Movie WHERE Movie.id = ?';
and default values (ticked checkboxes) which I get from the following SQL-query:
$sql = 'SELECT Genre.name FROM (Genre JOIN Movie2Genre ON Movie2Genre.idGenre = Genre.id) JOIN Movie ON Movie2Genre.idMovie = Movie.id WHERE Movie.id = ?';

If you have a solution or any simpler method I would love to hear you out. If you find anything here unclear, please tell me and I will do my best correct/add necessary information.
(PS: My if-statement is not working as I want it to do)

        <?php
        foreach ($allGenre as $val){
            foreach ($aktivGenre as $act) {
                if ($act==$val) {
                    $active = "checked";
                }else {
                    $active = "";
                }
            }


            echo "<input type='checkbox' name='{$val["name"]}' value='{$val["name"]}'".$active." >{$val["name"]}"; //$active="selected" when 
        }
        ?>