0

Update i have fix the first problem by changing the strectures in sending datas to database, now my problem is that when i select multiple choices and click submit only one of the choices is updated in database (exactly the first one ordering by id) my new Code is belew my old code, by the way i have add

echo $cam;

to verfy if its looping correctly and yes it does, the only issue is that in database it dont update all the choices entren only one of it

My orginal post

I'm trying to update a custom database table for my WordPress plugin. I'm firstly getting the roles from my table, then displaying it using a form. This works fine. When I check the boxes it updates in the database, but when I uncheck them, it doesn't update at all - and I get this error message:

Warning: Invalid argument supplied for foreach() in C:\wamp\www\wp_test\wp-content\plugins\Data\settings.php on line 52

Here's my code. Where am I going wrong?

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

?>
0

your variable $roles , either contains no value or is not an array, foreach only process an array/object.

  • nope that variable work fine, the problem is only that when i uncheck my chckbox it dont update in the database – Messi Adrinho Jul 22 '16 at 13:01
  • did you tried debugging by printing $cat or $_POST['cat'], after un-checking the check box, i believe you are not getting proper data on post back after un-check. @MessiAdrinho – bhupendraosd Jul 22 '16 at 16:27
  • yeah thats true, what i'm trying to do is that when i uncheck checkbox it will send the value 0 to update the value already exist in datatabe @bhupendraosd – Messi Adrinho Jul 22 '16 at 16:56
  • You have 2 foreach loops. Am I correct that your "line 52" error is for the second one - foreach ( $cats as $cam ) ? The error is to be expected - IF no checkbox is checked. Because, in that case, $_POST['cat'] is not set. Could this be the problem? – Jeff Cohan Jul 22 '16 at 18:55
  • yeah i have fix that already but the principal problem i'm having here is how can i send a value when box is unchecked @Jeff Cohan – Messi Adrinho Jul 22 '16 at 19:09
0

If no role is checked when you submit, the $_POST['cat'] will not be set. That will trigger the "invalid argument supplied for foreach" error. In the beginning of your

if ( isset($_POST['saveme']) ) 

structure, I'd put:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.