2014-10-02 95 views
0

我有3行,每行都有一個複選框,當我取消選中所有複選框並單擊保存時我在我的else語句中收到錯誤。如果我一次取消選中框並單擊保存,當我到達最後一個複選框並單擊保存時,我也會在else語句中收到錯誤。使用PHP PDO更新複選框的數據庫值不更新所有複選框

這是我的問題 - 我如何讓所有的複選框沒有收到錯誤沒有選中?

這是我的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' : '');?> /> 
+1

你真正的問題是什麼? – Mike 2014-10-02 15:34:45

+1

未選中的複選框不會在POST中發送。換句話說,除非你專門做了一些事情來解決這個問題,否則你永遠不會在POST數組中看到未經檢查的複選框。 – 2014-10-02 15:38:48

+1

[如何複選框狀態不總是傳遞給PHP腳本?](http://stackoverflow.com/questions/2520952/how-come-checkbox-state-is-not-always-passed-along- to-php-script) – 2014-10-02 15:39:54

回答

0

我增加了一個隱藏輸入的每一個。

所以我改變了這一點:

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

這似乎已經得到它的工作。