php复选框输入更新mysql数据库

时间:2014-09-11 04:08:47

标签: php mysql checkbox input

如果有人可以指出我正确的方向 将受到高度赞赏。由于某种原因数据库 未根据从复选框收到的输入更新。 下面是我用来完成此任务的代码片段 任务。

我不知道为什么,数据库中的内容保持不变 如果选中复选框,则两种方式都保持不变。

这是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 );
}
; ?>

非常感谢你们提出的任何意见。

4 个答案:

答案 0 :(得分:2)

您没有名为button

的输入

您的代码的执行成功基于整个条件语句。

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

它应该是:

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

根据您的提交按钮的名称:

<input type="submit" name="post_content"  value="Submit">
                           ^^^^^^^^^^^^

在打开<?php标记后立即将error reporting添加到文件顶部。

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

将发出Undefined index button...警告。

答案 1 :(得分:1)

而不是$_POST['button']您应该检查$_POST['post_content'],因为您的网页上没有按钮。您应该使用error_reporting

答案 2 :(得分:0)

如果未选中$_POST['drink']将不会被设置。所以检查是否检查过。

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

类似的东西然后

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

答案 3 :(得分: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,复选框不会像你所说的那样传递值。我假设你正在使用jquery

相关问题