使用循环问题在数据库中插入多个条目

时间:2016-07-22 作者:Messi Adrinho

Update我已经通过更改向数据库发送数据的结构来修复了第一个问题,现在我的问题是,当我选择多个选项并单击“提交”时,只有一个选项在数据库中更新(正是第一个按id排序的选项),我的新代码是belew我的旧代码,通过添加

echo $cam;
为了验证它的循环是否正确,是否正确,唯一的问题是,在数据库中,它不会更新所有选项,只会更新其中一个选项

My orginal post

我正在尝试更新WordPress插件的自定义数据库表。我首先从表中获取角色,然后使用表单显示它。这很好用。当我选中这些复选框时,它会在数据库中更新,但当我取消选中它们时,它根本不会更新-我会收到以下错误消息:

警告:为C:\\wamp\\www\\wp\\u test\\wp content\\plugins\\Data\\settings中的foreach()提供的参数无效。php在线52

这是我的密码。我哪里做错了?

<?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\'));

            }
    }

}

?>

UPDATE

<?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">
<fieldset>
<legend><strong>List of activated roles</strong></legend>
<ul>
  <?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\' => \'\'
                                   ));
            }?>

<?php if($results[0]->ADR_id==$results[0]->statut) {?>

<li><input type="checkbox" value="<?php echo $results[0]->ADR_id*2; ?>" name="rm[]" /><label><?php echo $results[0]->role;?></label></li>

<?php }} ?>
</ul>
<input type="submit" value="remove" name="remove" />
</fieldset>
</form>
<form action="<?php $_REQUEST[\'PHP_SELF\'] ?>" method="post">
<fieldset>
<legend><strong>List of deactivated roles</strong></legend>
<ul>
  <?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\' => \'\'
                                   ));
            }?>

<?php if($results[0]->ADR_id!=$results[0]->statut) {?>

<li><input type="checkbox" value="<?php echo $results[0]->ADR_id; ?>" name="ad[]" /><label><?php echo $results[0]->role;?></label></li>

<?php }} ?>
</ul>
<input type="submit" value="Add" name="add" />
</fieldset>
</form>
<?php
    if(isset($_POST[\'remove\']))
    {   if(isset($_POST[\'rm\'])){
        $cam=implode(",",$_POST[\'rm\']);
        $wpdb->update( $table_name, array( 
                                  \'statut\' => $cam 
                                   ),array(\'ADR_id\' => $cam/2),array(\'%d\'));
        echo $cam;
            }
     else{
         $cam=\'\';exit();}
    }
        if(isset($_POST[\'add\']))
    {   if(isset($_POST[\'ad\'])){
        $cam=implode(",",$_POST[\'ad\']);
        echo $cam;
        $wpdb->update( $table_name, array( 
                                  \'statut\' => $cam 
                                   ),array(\'ADR_id\' => $cam),array(\'%d\'));}
        else{
         $cam=\'\';exit();
         }
    }

?>

2 个回复
SO网友:bhupendraosd

您的变量$roles , 不包含值或不是数组,foreach只处理数组/对象。

SO网友:Jeff Cohan

如果no role 如果在提交时选中,则不会设置$\\u POST[\'cat\']。这将触发“为foreach提供的参数无效”错误。在您的

if ( isset($_POST[\'saveme\']) ) 
结构,我认为:

if ( !isset($_POST[\'cat\']) ) {
  // code to update db value to 0
}