Update database values ​​for checkboxes using PHP PDO without updating all checkboxes

I have 3 lines and each line has a checkbox, when I uncheck all the checkboxes and click save. I am getting an error in the else statement. If I uncheck the box and click Save, when I get to the last checkbox and click Save, I also get an error in the else statement.

Here's my question. How do I allow all checkboxes to be checked without getting an error?

Here is my PHP code:

//Update Social Preferences with new value for display on homepage
$settingsArray = array('myfacebook', 'mytwitter', 'mygoogle');
if(isset($_POST['btn-save']))
{          
      if(isset( $_POST['mysocial']))
      {
          $values = array();
      foreach($_POST['mysocial'] as $selection )
      {  if(in_array($selection, $settingsArray))
         {  $values[ $selection ] = 1; }
         else
         {  $values[ $selection ] = 0; }
      } // end of foreach.

      $user_id = $_SESSION['user']['id'];

      try // save user selection to the database
      {

        $stmt = $db->prepare("UPDATE social_preferences SET my_facebook = :myfacebook, my_twitter = :mytwitter, my_google = :mygoogle WHERE user_id = :userID");
        $stmt->bindParam(":userID", $userid, PDO::PARAM_INT);
        $stmt->bindParam(':myfacebook', $values['myfacebook']);
        $stmt->bindParam(':mytwitter', $values['mytwitter']);
        $stmt->bindParam(':mygoogle', $values['mygoogle']);
        $stmt->execute();

        header("Location: admin-social-test.php"); 
        die("Redirecting to admin-social-test.php"); 
       }  catch(PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); }
  }
  else
  {  $noCheckbox = 'No checkbox selection made...'; }     
} // End of, if statement from the button check 

      

Here is my HTML code:

<input type="checkbox" name="mysocial[]" value="myfacebook" <?php echo ($result['my_facebook']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mytwitter" <?php echo ($result['my_twitter']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mygoogle" <?php echo ($result['my_google']==1 ? 'checked' : '');?> />

      

0


source to share


1 answer


I have added hidden input for each one.

So, I change this:

<input type="checkbox" name="mysocial[]" value="myfacebook" <?php echo ($result['my_facebook']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mytwitter" <?php echo ($result['my_twitter']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mygoogle" <?php echo ($result['my_google']==1 ? 'checked' : '');?> />

      



For this:

<input type="hidden" name="mysocial[]" value="myfacebook1" <?php echo ($result['my_facebook']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="myfacebook" <?php echo ($result['my_facebook']==1 ? 'checked' : '');?> />
<input type="hidden" name="mysocial[]" value="mytwitter1" <?php echo ($result['my_twitter']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mytwitter" <?php echo ($result['my_twitter']==1 ? 'checked' : '');?> />
<input type="hidden" name="mysocial[]" value="mygoogle1" <?php echo ($result['my_google']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mygoogle" <?php echo ($result['my_google']==1 ? 'checked' : '');?> />

      

This seems to work.

0


source







All Articles