使用多个列表菜单和复选框php

Am retrieving some data from the DB and am allowing users to make multiple selection via check box and also selecting a level for each selected check box. when saving, i only get to see the selected check boxes in the DB but not the level selected.

Code for making selection

include ('mysql_connect.php');

$sql = mysql_query("SELECT * FROM competency WHERE department = '$department'");


while($row = mysql_fetch_array($sql))
{
echo "<tr>";
echo "<td>";
echo"<input type='checkbox' name='comp[]' value= ".$row['id']." /> ".$row['competency']." <br /> </td>";
echo"<td> <select name='level[]'value= ".$row['id']." >
            
                  <option></option>
                  <option>level 1</option> 
                  <option>level 2</option> 
                  <option>level 3</option> 
                  <option>level 4</option>
                  <option>level 5</option>  </select>                 </td> ";


}
echo "</tr>";
?>

<input name="submit" type="submit" value="submit" />
</form>
<?php
echo" </table>";

?>

.. Code for saving into the DB

session_start();
$id = $_SESSION['user_id'];

$hobb = $_POST['comp'];
$level  = $_POST['level'];
include ('mysql_connect.php');



 $N = count($hobb);

        
        for($i=0; $i < $N; $i++)
        {
            
            
            
            $var1=$hobb[$i];
            $var2 =  $level[$i];
            //include ('connect.php');
            include ('mysql_connect.php');
            $table = "INSERT INTO competency_result (user_id,competency_id,level) ".
                     "VALUES ('$id', '$var1', '$var2')";
            mysql_query($table) or die(mysql_error());
            $inserted_fid = mysql_insert_id();
            mysql_close();  
        }

</div>

Set the key the same for comp[] and level[], ie. $row['id'] -

while($row = mysql_fetch_array($sql))
{
...[your code]...

echo "<input type='checkbox' name='comp[".$row['id']."]' value= ".$row['id']." /> ".$row['competency']." <br /> </td>";
echo "<td> <select name='level[".$row['id']."]' >
...[your code]...
}

(note: the <select> does not have a value attribute. That is defined by the <option>)

Then you can easily get the corresponding values using

foreach($hobb as $key=>$val)
{
    $var1=$hobb[$key];
    $var2=$level[$key];
    ...[your code]...
}

(also, it looks like you have <tr> inside your loop, but </tr> after. I would assume you would want to move the closing tag inside the loop to properly close each row)