Updating user status with checkbox
Hi everyone!
I need some code help for a problem that i am stuck with. The issue is to update the user table which has a status field which can contain only "YES" or "NO".
My 1st page shows all the listed users with a check box beside them. if the users are active (i.e database contains a "YES" value) then the checkbox are checked otherwise unchecked. If I uncheck any text box that user's value becomes "NO" in the database.
The checkbox looks like:
<input type="checkbox" name="ch<?=$i?>" value="<?=$memdata['adv_id']?>" <?if ($memdata['usr_status']=='YES') {?> checked <?}?>>
** i is the total no. of records
** $memdata['adv_id'] is the user id which is passed through the checkbox
I am using a hidden field to keep the count of check box.
<input type="hidden" name="txtcount" value="<?=$i?>">
** i is the total no. of checkbox & hence the tot. no. of records.
I am using the 2nd page for processing:Here is the code I am using to update the status of the user --
--------------------------------
if( $_POST['txtcount'] != "" )
{
$chcount = $_POST['txtcount'];
}
for($i = 0;$i < $chcount; $i++)
{
if($_POST['ch'.$i] != "" )
{
$up_query = mysql_query("update srch_adv_contact set usr_status = 'YES' where adv_id = '".$_POST['ch'.$i].
"'");
}
else
{
$up_query = mysql_query("update srch_adv_contact set usr_status = 'NO' where adv_id <> '".$_POST['ch'.$i].
"'");
}
}
--------------------------------------------------------------
With this code if I don't put the else part I can change the status once but I cannot revoke the status again because once I uncheck any of the checkbox no value is updated. With the else clause no status is changed.
Please help me in this situation!!
|