使用循环形式的复选框更新mysql数据表

时间:2016-07-22 07:43:26

标签: php mysql wordpress

我试图更新我的wordpress插件的自定义表,首先我从我的表中获取角色,然后使用表单显示它,这工作正常,现在问题是,当我检查它更新的框在数据库中,但是当我取消选中它时,它根本不更新,如果我取消选中它,我会收到此错误消息

  

警告:第52行的C:\ wamp \ www \ wp_test \ wp-content \ plugins \ Data \ settings.php中为foreach()提供的参数无效

<?php 
$roles=get_editable_roles();

global $wpdb;
$table_name = $wpdb->prefix. "Author_detailed_repport";
?>

<h3>Settings Page</h3>
<h4>Add/Remove a role from filter list</h4>
<p>This setting allow you to add/remove roles from the filter<br />
  list, here down a list of all the roles existing in your website, all<br />
  you have to do is to check/uncheck wich you wanna add/rmove from filter list</p>
<form action="<?php $_REQUEST['PHP_SELF'] ?>" method="post">
  <?php 
require_once('../wp-config.php');
$i=0;
foreach($roles as $role)
{

     $rom=$role['name'];
     $results = $wpdb->get_results( "SELECT * FROM  ".$table_name." WHERE role= '".$rom."'" );
     if ($results==NULL)
            {$wpdb->insert( $table_name, array( 
                                  'role' => $role['name'],
                                  'statut' => '', 
                                  'post_number' => '', 
                                  'activate' => ''
                                   ));
            }?>
  <input type="checkbox" name="cat[]" value="<?php echo $i+1 ;?>" <?php checked($results[0]->statut, $i+1); ?> />
 <input type="hidden" name="ww" value="0">
   <?php 


 ?>
  <label>
  <?php echo $results[0]->role;?></label><br />

  <?php $i++; } ?>
  <input type="submit" value="save" name="saveme" />
</form>
<?php 


if(isset($_POST['saveme']))
{
    $cats=$_POST['cat'];
    foreach($cats as $cam)
    {
        if(isset($cam))
        {
        $wpdb->update( $table_name, array( 
                                  'statut' => $cam 
                                   ),array('ADR_id' => $cam),array('%d'));}
        else
        {
             $wpdb->update( $table_name, array( 
                                  'statut' => '0' 
                                   ),array('ADR_id' => $cam),array('%d'));

            }
    }

}

?>

1 个答案:

答案 0 :(得分:0)

因为$ roles尚未设置,您需要进行另一次检查

If(isset($roles)) {
//foreach loop goes in here
}