table :
role_id role_name perm_id
1 admin 0
2 Manager 0
7 Accounts Assitant 4,6
8 Registrar 2,5,6
Iam storing all these information from a PHP Form
<form action="" method="post">
<table>
<tr><th>ROLE</th>
<th>PERMISSIONS<br><input type="checkbox" class="chk_boxes"></th>
</tr>
<tr>
<td><input type="text" name="role_name" required></td>
<td><input type="checkbox" class="chk_boxes1" name="perm[]" value="1">My Account<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="2">Edit Account<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="3">Change password<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="4">List of users<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="5">Define roles<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="6">Assign roles<br>
</td>
</tr></table>
<div><input type="submit" name="submit" value="create role"></div>
</form>
I was succesfully submitting the data to database, I had issues while retrieving the checkbox values from database. Now I want to edit this form and show the checkboxes(selected one's and deselected one's) also.
<?php
$query = $db->prepare("SELECT * FROM role WHERE role_id = ".$role_id." ");
$query->execute();
foreach($query as $q)
{
echo '<form action="" method="post">
<table>
<tr><th>ROLE</th>
<th>PERMISSIONS<br><input type="checkbox" class="chk_boxes"></th>
</tr>
<tr><td><input type="text" value="'.$q['role_name'].'" name="role_name" required></td>
<td>';
$permid_array = $q['perm_id'];
foreach(explode(',', $permid_array) as $n)
{
if ($n == 1 || $n == 2 || $n == 3 || $n == 4 || $n == 5 || || $n == 6) { $set_checked = "checked";}
else {$set_checked = ""; }
echo '
<input type="checkbox" class="chk_boxes1" name="perm[]" value="1" '.$set_checked.' > My Account<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="2" '.$set_checked.' > Edit Account<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="3" '.$set_checked.' > Change Password<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="4" '.$set_checked.' > List of users<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="5" '.$set_checked.' > Define roles<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="6" '.$set_checked.' > Assign roles<br>';
}
echo '</td></tr>
</table>
<div><input type="submit" name="submit" value="create role"></div>
</form>';
}
?>
When I execute the edit form, the checkboxs are getting repeated. Suggest me where I went wrong or guide me other way of doing.
Many Thanks