【问题标题】:php checkbox input update mysql databasephp复选框输入更新mysql数据库
【发布时间】:2014-11-04 21:35:25
【问题描述】:

如果有人能指出我正确的方向 将不胜感激。由于某种原因,数据库 根据从复选框收到的输入,未更新。 下面是我用来完成此操作的代码的 sn-p 任务。

我不知道为什么,数据库中的内容停留在 如果复选框被选中,无论哪种方式都不变。

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

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

【问题讨论】:

  • 您没有名为 button 的输入 - 整个条件语句都基于它。提示:post_content
  • 你应该使用 if(isset($_POST['drink'])) { }。祝你好运!

标签: php mysql checkbox input


【解决方案1】:

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

【讨论】:

    【解决方案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">
                               ^^^^^^^^^^^^
    

    error reporting 添加到文件顶部,紧跟在打开&lt;?php 标记之后。

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

    这将引发Undefined index button... 警告。

    【讨论】:

      【解决方案3】:
      <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

      【讨论】:

        【解决方案4】:

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

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

        类似的,然后

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

        【讨论】:

          猜你喜欢
          • 2012-08-16
          • 1970-01-01
          • 2016-12-14
          • 2012-06-03
          • 2017-06-06
          • 2012-08-30
          • 1970-01-01
          • 2014-05-22
          • 1970-01-01
          相关资源
          最近更新 更多