php - 使用 PHP PDO 更新复选框的数据库值,但不更新所有复选框

标签 php mysql checkbox pdo

我有 3 行,每行都有一个复选框,当我取消选中所有复选框并单击“保存”时,我在 else 语句中收到错误。如果我一次取消选中某个框并单击“保存”,当我到达最后一个复选框并单击“保存”时,我还会在 else 语句中收到错误。

这是我的问题 - 如何允许取消选中所有复选框而不收到错误?

这是我的 PHP 代码:

//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 

这是我的 HTML 代码:

<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' : '');?> />

最佳答案

我为每个输入添加了一个隐藏输入。

所以我改变了这个:

<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' : '');?> />

对此:

<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' : '');?> />

这似乎已经成功了。

关于php - 使用 PHP PDO 更新复选框的数据库值,但不更新所有复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26164244/

相关文章:

php - 创建折扣代码系统 (MySQL/php)

mysql - IntelliJ 15/Grails 3 : Cannot connect to mySql DB

javascript - 操作后验证 DOM 中不存在属性 - Protractor

c# - 如何在 C# 中关闭程序后保存复选框、文本框和...的状态

php - File_get_contents 函数不适用于 Windows?

php上传文件错误代码3

php - 无法使用php向ios发送推送通知

mysql - 如何从其他表中获取名称为 "same"的所有值的客户

mysql - 将 CSV 文件转换为 Mysql (.sql) 数据库文件?

javascript - var 检查 = $ ("input[no]").is (":checked")?2 :1; and how to reach 2nd database row