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