zero-perfoliate
zero-perfoliate

Author Topic: Checkboxes: unchecked troubles!  (Read 351 times)

Offline em123

  • New PHP Members
  • Posts: 1
  • Karma: +0/-0
Checkboxes: unchecked troubles!
« on: April 03, 2009, 11:48:06 AM »
Hey guys, just wondered if anyone could help please. I am new to php. I am trying to update by database so that if any check boxes are checked then update to 'yes' and the check boxes that were not checked to update to 'no' .
my code for creating the check boxes is:

Code: [Select]
print "<td align = \"CENTER\" bgcolor=\"$color\">" . "<input type=\"checkbox\"  name=\"fchk1[]\"  value=\"$doc_no\" echo '$checked' >
and my code for the action page is:
Code: [Select]
if(isset($_POST['update'])) {
$fchk1=$_POST['fchk1'];
if (is_array($fchk1)){
      foreach ($fchk1 as $doc_no){
        $update_query = "UPDATE uploads u SET u.saccess = 'yes' WHERE  u.doc_no = '$doc_no'";
        $result = $db->query($update_query);
        //$db->disconnect();
        header("Location: main.php?target=docs&type=h");
            }
   }
else {

   $update_query = "UPDATE uploads SET saccess = 'no'";
   $result = $db->query($update_query);
   //$db->disconnect();
   header("Location: main.php?target=docs&type=h");
}
}


the checked part works fine, but the unchecked part does not work unless there is no checked boxes.

Thanx Emma

Offline PhPHelper

  • http://Digiscapers.com
  • Full Member
  • PHP Problem Solvers
  • ****
  • Posts: 179
  • Karma: +50/-0
    • Bad Apple Mail
Re: Checkboxes: unchecked troubles!
« Reply #1 on: April 07, 2009, 12:21:38 AM »
try:

Code: [Select]
if(isset($_POST['update'])) {
$fchk1=$_POST['fchk1'];
if (is_array($fchk1)){
      foreach ($fchk1 as $doc_no){
        $update_query = "UPDATE uploads u SET u.saccess = 'yes' WHERE  u.doc_no = '$doc_no'";
        $result = $db->query($update_query);
        //$db->disconnect();
        header("Location: main.php?target=docs&type=h");
            }
   }
if (!in_array($fchk1)){

   $update_query = "UPDATE uploads SET saccess = 'no'";
   $result = $db->query($update_query);
   //$db->disconnect();
   header("Location: main.php?target=docs&type=h");
}
}