PHP Wordpress 复选框问题

PHP Wordpress checkbox issue

我正在使用样板框架 (https://wppb.me/) 构建一个 wordpress 插件。所有作品都能找到。我有一个烦人的问题,我无法解决。

我有一个带有布尔字段的 table AM_ACTIVE

在页面布局中,我希望这是一个复选框。请参阅下面的方法。初始化一个数组,并使用 shortcode_atts 合并数组和请求参数。对于添加新的这个作品。但是如果我编辑它。编辑页面显示 checked/non 已正确检查。但是保存总是会导致错误。我已经尝试了一百万件事。 True/false 0/1。将值与缓存值、项目值、请求值放在一起。 大多数时候我会收到 PHP 警告:在 /var/www/vhosts/marcelvreuls.eu/httpdocs/wp-content/plugins/am

中遇到非数字值

所以我假设复选框不知道什么时候没有被触摸或什么的。我没有任何线索。谁能帮帮我。我只想activated/deactived一个记录。

任何帮助将不胜感激

先是页面代码,后是保存代码

    function amarre_maingroup_form_meta_box_handler($item)
    {
        ?>
        <table cellspacing="2" cellpadding="5" style="width: 100%;" class="form-table">
            <tbody>
            <tr class="form-field">
                <th valign="top" scope="row">
                    <label for="am_name"><?php _e('Name', 'amarre')?></label>
                </th>
                <td>
                    <input id="am_name" name="am_name" type="text" style="width: 50%" value="<?php echo esc_attr($item['am_name'])?>"
                           size="50" class="code" placeholder="<?php _e('am_maingroup', 'amarre')?>" required>
                </td>
            </tr>         
            <tr class="form-field">
                <th valign="top" scope="row"> 
                    <label for="am_url"><?php _e('Afbeelding', 'amarre')?></label>
                </th>
                <td>
                    <input id="am_url" name="am_url" type="text" style="width: 50%" value="<?php echo esc_attr($item['am_url'])?>"
                           size="50" class="code" placeholder="<?php _e('Afbeelding', 'amarre')?>" required>
                </td>
            </tr>        
             <tr class="form-field">
                <th valign="top" scope="row">
                    <label for="am_active"><?php _e('am_active', 'amarre')?></label><?php echo $_POST['am_active']?>
                </th>
                <td>
                <?php 
                if ($item['am_active'] == 1)
                {
                    echo('<input name="am_active" type="checkbox"  value="1" checked="checked">');
                }
                else
                {
                    echo('<input name="am_active" type="checkbox"  value="0">' );
                }              
                ?>
                </td>
            </tr>      
            </tbody>
        </table>
        <?php
    }
function amarre_maingroup_form_page_handler()
    {
        global $wpdb;
        $table_name = $wpdb->prefix . 'am_maingroup';

        $message = '';
        $notice = '';

        // this is default $item which will be used for new records
        $default = array(
            'id' => 0,
            'am_name' => '',
            'am_active' => '',
            'am_url' => ''
        );

        // here we are verifying does this request is post back and have correct nonce
        if ( isset($_REQUEST['nonce']) && wp_verify_nonce($_REQUEST['nonce'], basename(__FILE__))) {
            // combine our default item with request params
            $item = shortcode_atts($default, $_REQUEST);

            error_log( implode( ", ", $_REQUEST ));
            error_log( implode( ", ", $item ));
            // validate data, and if all ok save item to database
            // if id is zero insert otherwise update
            $item_valid =   $this->amarre_validate_maingroup($item);
            if ($item_valid === true) {  
                error_log( $item['am_name']);    
                error_log( $_REQUEST['am_active']);              
                if ($item['id'] == 0) {
                    $result = $wpdb->insert($table_name, $item);
                    $item['id'] = $wpdb->insert_id;
                    if ($result) {
                        $message = __('Item was successfully saved', 'amarre');
                    } else {
                        $notice = __('There was an error while saving item' + $result, 'amarre');
                    }
                } else {
                    $result = $wpdb->update($table_name, $item, array('id' => $item['id']));
                    if ($result) {
                        $message = __('Item was successfully updated', 'amarre');
                    } else {
                        $notice = __('There was an error while updating item' + $result, 'amarre');
                    }
                }
            } else {
                // if $item_valid not true it contains error message(s)
                $notice = $item_valid;
            }
        }
        else {
            // if this is not post back we load item to edit or give new one to create
            $item = $default;
            if (isset($_REQUEST['id'])) {
                $item = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE id = %d", $_REQUEST['id']), ARRAY_A);
                if (!$item) {
                    $item = $default;
                    $notice = __('Item not found', 'amarre');
                }
            }
        }

通过在验证部分添加一个方法来解决它。如果为空,则将其设置为 0。因为我注意到如果布尔值不是 set/touched,它将没有任何值