Getting Checkbox ticked from DB values

I have an edit table where I want the check boxes to be ticked if that option was previously saved in the database. I am only getting one box ticked (the very first selected item in the table), the other saved items after that dont get ticked.
The project, is one that stores module information for students. As such the tblmodule_student table was created because there was a many to many relationship between my tblstudent table and tblmodules table.

It has been four weeks I have tried various things to get this working. in the time I was able to solve by similar drop down box selection issue, and i am trying to solve this one now.

I have verified that $nameset[‘StudentId’] has the correct information
$stu_modul_arr is storing the correct information from the database
$Mresult is getting all the module information from the database
the echo command for the checkboxes is displaying the modules and check boxes.

[php] $Mquery = "
SELECT ModuleId
FROM tblmodule_student
WHERE StudentId = {$nameset[‘StudentId’]}";
$Sresult_set = mysql_query($Mquery, $db);
confirm_query($result_set);
$stu_modul_arr = mysql_fetch_array($Sresult_set);

                                 $Mresult_set = get_all_modules();
                                   while($row = mysql_fetch_array($Mresult_set))
                                   {
                                   $s = $row['ModuleId'];
                                   if (in_array($s, $stu_modul_arr)) {
                                    $sel = "checked";
                                   } else {
                                    $sel = " ";   
                                   }
                                   //$sel = ($nameset['Role'] == $s) ? 'selected = "selected"' : '';
                                   echo "<p align=\"left\"> <input type= \"checkbox\" name=\" moduleselected[]\" value=" .$row['ModuleId'] ." $sel>" .$row['ModuleName'] .""; 
                                   } 
                                  [/php]

This fixed my problem

[php] $Mquery = "
SELECT ModuleId
FROM tblmodule_student
WHERE StudentId = {$nameset[‘StudentId’]}";
$Sresult_set = mysql_query($Mquery, $db);
confirm_query($Sresult_set);

                                    $stu_modul_arr = array();
                                    while($row = mysql_fetch_array($Sresult_set))
                                    {
                                        array_push($stu_modul_arr, $row['ModuleId']);
                                    }
                              $Mresult_set = get_all_modules();
                                   while($row = mysql_fetch_array($Mresult_set))
                                   {
                                   $s = $row['ModuleId'];
                             //if the modules saved for the student is found in the table
                                   if (in_array($s, $stu_modul_arr)) 
                                    {
                                    $sel = "checked=\"checked\"";
                            //if the modules were not previously saved
                                   } else {
                                    $sel = " ";   
                           
                                   }
                                   //$sel = ($nameset['Role'] == $s) ? 'selected = "selected"' : '';
                                   echo "<p align=\"left\"> <input type= \"checkbox\" name=\" moduleselected[]\" value=" .$row['ModuleId'] ." $sel>" .$row['ModuleName'] .""; 
                                   } [/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service