php mysql parallel array checkboxes
        Posted  
        
            by gramware
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by gramware
        
        
        
        Published on 2010-03-24T15:44:13Z
        Indexed on 
            2010/03/24
            15:53 UTC
        
        
        Read the original article
        Hit count: 377
        
I have an array of checkboxes that I edit at once to set up a 'tinyint' field. the problem comes in when i uncheck the checkbox and post the vales to mysql. since it posts an array of checkboxes and another parallel array of values to edit, unchecking a checkbox results in the 0 value been ignored by PHP_POST and hence the checkbox array will be less by the number of unchecked values in the form while the array to be edited will have all the records in the form.
here is the submit code
while($row=mysql_fetch_array($result))
{
$checked = ($row[active]==1) ? 'checked="checked"' : '';
...
echo "<input type='hidden' name='TrID[]' value='$TrID'>";
echo "<input type='checkbox' name='active1[]' value='$row[active]''$checked' >";
...
and the processing php script
$userid = ($_POST['TrID']);
$checked= ($_POST['active']);
$i=0;
foreach ($userid as $usid) 
{
if ($checked[$i]==1){
$check = 1;
}
else{
$check = 0;
}
$qry1 ="UPDATE  `epapers`.`clientelle` SET  `active` =  '$check' WHERE  `clientelle`.`user_id` =  '$usid' ";
$result = mysql_query($qry1);   
$i++;
}
© Stack Overflow or respective owner