2014-09-11 155 views
-1

如果有人能指出我在正確的方向 將不勝感激。出於某種原因,數據庫 未根據從複選框收到的輸入進行更新。 下面是我用來完成這個 任務的代碼片段。php複選框輸入更新mysql數據庫

我不知道爲什麼,數據庫中的內容保持 相同的方式,如果複選框被選中或不選。

這是HTML代碼

<form method="post" action=""> 
    <fieldset> 
     <input type="hidden" name="Jack" value="<?php echo $post_id; ?>"> 
     <input type="checkbox" name="drink" value="1"> <span>Did you drink today?</span>    
     <input type="submit" name="post_content" value="Submit"> 

     </div> 
    </fieldset> 
</form> 

這是PHP

<?php   

if (isset($_POST['button']) && (($_POST['drink']) == 1)) { 

    $post_id = $_POST['Jack']; 
    $my_post = array(
       'ID' => $post_id, 
       'post_content' => 'Just whiskey' 
       ); 
    wp_update_post($my_post); 
} 
; ?> 

非常感謝任何輸入你們會做。

+2

您沒有名爲'button'的輸入 - 整個條件語句都是基於它的。提示:'post_content' – 2014-09-11 04:13:50

+0

你應該使用if(isset($ _ POST ['drink'])){}。祝你好運! – tungbk29 2014-09-11 04:47:15

回答

0

如果沒有檢查$_POST['drink']將不會被設置。所以檢查它是否被檢查。

$drink = isset($_POST['drink']) ? $_POST['drink'] : 0; 

類似的東西,然後

if (isset($_POST['button']) && ($drink' == 1)) { 
2

您不必輸入命名button

你的代碼的執行的成功是基於整個條件語句。

if (isset($_POST['button']) && (($_POST['drink']) == 1)) {...} 
        ^^^^^^ 

它應該是:根據您提交按鈕的名稱

if (isset($_POST['post_content']) && (($_POST['drink']) == 1)){...} 

​​

添加error reporting到您的文件(S)的頂部您打開後立即<?php標記。

error_reporting(E_ALL); 
ini_set('display_errors', 1); 

這將拋出Undefined index button...警告。

0
<form method="post" action=""> 
    <fieldset> 
     <input type="hidden" name="Jack" value="<?php echo $post_id; ?>"> 
     <input type="hidden" name="drink"> 
     <input type="checkbox" class='check_or_not'> <span>Did you drink today?</span>    
     <input type="submit" name="post_content" value="Submit"> 
    </fieldset> 
</form> 
<script> 
$(".check_or_not").click(function(){ 
    if($(this).attr('checked') == 'checked'){ 
     $(this).prev().val(1); 
    }else{ 
     $(this).prev().val(0); 
    } 
}) 
</script> 

改變你的HTML像上面,複選框,將無法通過像你excepted..i你正在使用jQuery

1

,而不是$_POST['button']你應該檢查$_POST['post_content']因爲上沒有按鈕假設什麼價值你的page.you應該使用error_reporting