| | |
Checkbox Arrays ARGH!!
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
Hello all,
I have a form that contains checkboxes to update a database with either "Yes" or "No" based on the user selection.
The check boxes are in an array with the values set to a "LinkID"
example:
I have the following insert statement that works as long as the values are "Yes":
However, I need this to also update the datebase if the user unchecks the checkbox (BTW there are 4 checkboxes "Add", "Update" "Delete", and "View") or the checkbox has not been selected.
Can anyone point me in the right direction ?
Many thanks in advance!
I have a form that contains checkboxes to update a database with either "Yes" or "No" based on the user selection.
The check boxes are in an array with the values set to a "LinkID"
example:
php Syntax (Toggle Plain Text)
<?php //find the user's assigned links $rowlink=$row_rsUpdate['LinkID']; mysql_select_db($database_mambo, $mambo); $query_rsAssignedLinks = "SELECT * FROM assignedlinks WHERE username = '$username' and ALID = '$rowlink' and `Add` ='Yes'"; $rsAssignedLinks = mysql_query($query_rsAssignedLinks, $mambo) or die(mysql_error()); $row_rsAssignedLinks = mysql_fetch_assoc($rsAssignedLinks); $totalRows_rsAssignedLinks = mysql_num_rows($rsAssignedLinks); ?> <input type="checkbox" name="Add[]" value="<? echo $row_rsUpdate['LinkID']; ?>" <?php if (!$_POST && (!(strcmp($row_rsAssignedLinks['Add'],"Yes")))) { echo "checked=\"checked\"";} ?> <?php if ($_POST && (isset($_POST['Add'])=="1")) { echo "checked=\"checked\"";} ?> <?php $OK = isset($_POST['Add']) ? true : false; if($OK && $_POST['Add']=="Yes") { ?> <? } ?>/> Add
I have the following insert statement that works as long as the values are "Yes":
php Syntax (Toggle Plain Text)
if((isset($_POST["Submit"])) && ($_POST["MM_Update"] == "update")) { for($m=0;$m<count($_POST['Add']);$m++) { $add=$_POST['Add'][$m]; //find the correct MenuID mysql_select_db($database_mambo, $mambo); $query_rsMenuID = "SELECT * FROM `links` WHERE LinkID = '$add'"; $rsMenuID = mysql_query($query_rsMenuID, $mambo) or die(mysql_error()); $row_rsMenuID = mysql_fetch_assoc($rsMenuID); $totalRows_rsMenuID = mysql_num_rows($rsMenuID); $mid=$row_rsMenuID['MenuID']; //find if there is already an assignedlink record written for the ALID mysql_select_db($database_mambo, $mambo); $query_rsALFind = "SELECT * FROM assignedlinks WHERE mambo_user_id = '$id' and `username` = '$username' AND ALID = '$add' AND `Add` = 'Yes'"; $rsALFind = mysql_query($query_rsALFind, $mambo) or die(mysql_error()); $row_rsALFind = mysql_fetch_assoc($rsALFind); $totalRows_rsALFind = mysql_num_rows($rsALFind); (!empty($_POST["Add"])) { while(list($k, $v) = each($_POST["Add"])) { $v= "Yes"; foreach($_POST['Add'] as $row=> $assigned) { $assigned= $assigned; $MenuID = $_POST['menu'][$row]; $updateAddlinksYes = "UPDATE assignedlinks SET `Add`='$v' WHERE ALID='$assigned' AND `mambo_user_id`= '$id'"; $ResultAddlinksYes = mysql_query($updateAddlinksYes, $mambo) or die(mysql_error()); echo $updateAddlinksYes; }
However, I need this to also update the datebase if the user unchecks the checkbox (BTW there are 4 checkboxes "Add", "Update" "Delete", and "View") or the checkbox has not been selected.
Can anyone point me in the right direction ?
Many thanks in advance!
I keep hitting "escape", but I'm still here!!!!
:}
:}
When checkboxes are sent in requests as "on" if they are checked, if they are not checked they aren't sent at all. You're also missing an if on line 20
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Thanks for the input. I do see that the "if" is missing as I was doing the "cut and paste thing".
I understand what you are saying there, but unsure right now on how to exactly write what I need it to do..
basically the concept here is
I display the users assigned menus along with 4 checkboxes (Add, Update, Delete, View)...These checkboxes are an aray populated with the values contained within the db. Additionally, this checkbox array is given a value that is a constant within the repeating region.
ex:
what I want to do is:
if the user changes any values in the checkbox array, I need the db updated with all changes. I used the constant value within the array to maintain the correct record to be updated on change.
I have it working to updating and added yes values, but keep getting "stumped" on the next part...;}
any example you can thing of would me most appreciated
I understand what you are saying there, but unsure right now on how to exactly write what I need it to do..
basically the concept here is
I display the users assigned menus along with 4 checkboxes (Add, Update, Delete, View)...These checkboxes are an aray populated with the values contained within the db. Additionally, this checkbox array is given a value that is a constant within the repeating region.
ex:
php Syntax (Toggle Plain Text)
<input type="checkbox" name="Add[]" value="<? echo $row_rsUpdate['LinkID']; ?>"<?php if (!$_POST && (!(strcmp($row_rsAssignedLinks['Add'],"Yes")))) { echo "checked=\"checked\"";} ?> <?php if ($_POST && (isset($_POST['Add'])=="1")) { echo "checked=\"checked\"";} ?> <?php $OK = isset($_POST['Add']) ? true : false; if($OK && $_POST['Add']=="Yes") { ?> <? } ?>/> Add <?php //find the user's assigned links $rowlink=$row_rsUpdate['LinkID']; mysql_select_db($database_mambo, $mambo); $query_rsAssignedLinks = "SELECT * FROM assignedlinks WHERE username = '$username' and ALID = '$rowlink' and `Add` ='Yes'"; $rsAssignedLinks = mysql_query($query_rsAssignedLinks, $mambo) or die(mysql_error()); $row_rsAssignedLinks = mysql_fetch_assoc($rsAssignedLinks); $totalRows_rsAssignedLinks = mysql_num_rows($rsAssignedLinks); ?> <input type="checkbox" name="Add[]" value="<? echo $row_rsUpdate['LinkID']; ?>" <?php if (!$_POST && (!(strcmp($row_rsAssignedLinks['Add'],"Yes")))) { echo "checked=\"checked\"";} ?> <?php if ($_POST && (isset($_POST['Add'])=="1")) { echo "checked=\"checked\"";} ?> <?php $OK = isset($_POST['Add']) ? true : false; if($OK && $_POST['Add']=="Yes") { ?> <? } ?>/> Add
what I want to do is:
if the user changes any values in the checkbox array, I need the db updated with all changes. I used the constant value within the array to maintain the correct record to be updated on change.
I have it working to updating and added yes values, but keep getting "stumped" on the next part...;}
any example you can thing of would me most appreciated
Last edited by JeniF; Feb 27th, 2008 at 9:50 pm. Reason: typo
I keep hitting "escape", but I'm still here!!!!
:}
:}
•
•
•
•
if the user changes any values in the checkbox array, I need the db updated with all changes.
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
Hey there,
Yes after my "Brain-Cloud" left me for the day, I realized that I needed to delete the original values stored and reset them with the new values.
I managed to get this done.
Thanks for the "heads-up" on that one...
If anyone is interested in the code, let me know and I will post it
Yes after my "Brain-Cloud" left me for the day, I realized that I needed to delete the original values stored and reset them with the new values.
I managed to get this done.
Thanks for the "heads-up" on that one...
If anyone is interested in the code, let me know and I will post it
I keep hitting "escape", but I'm still here!!!!
:}
:}
Here is the code that I used and tested:
php Syntax (Toggle Plain Text)
<?php require_once('../Connections/mambo.php'); ?> <?php error_reporting(E_ALL ^ E_NOTICE); session_start(); if((isset($_POST["Submit"])) && ($_POST["MM_Update"] == "update")) { if(!empty($_POST["Add"])) { while(list($k, $v) = each($_POST["Add"])) { $v= "Yes"; foreach($_POST['Add'] as $row=> $assigned) { $assigned= $assigned; $MenuID = $_POST['menu'][$row]; $updateAddlinksYes = "UPDATE assignedlinks SET `Add`='$v' WHERE ALID='$assigned' AND `mambo_user_id`= '$id'"; $ResultAddlinksYes = mysql_query($updateAddlinksYes, $mambo) or die(mysql_error()); echo $updateAddlinksYes; } } } //// if(!empty($_POST["Update"])) { while(list($k, $v) = each($_POST["Update"])) { $v= "Yes"; foreach($_POST['Update'] as $row=> $assigned) { $assigned= $assigned; $MenuID = $_POST['menu'][$row]; $updateAddlinksYes = "UPDATE assignedlinks SET `Update`='$v' WHERE ALID='$assigned' AND `mambo_user_id`= '$id'"; $ResultAddlinksYes = mysql_query($updateAddlinksYes, $mambo) or die(mysql_error()); echo $updateAddlinksYes; } } } // if(!empty($_POST["Delete"])) { while(list($k, $v) = each($_POST["Delete"])) { $v= "Yes"; foreach($_POST['Delete'] as $row=> $assigned) { $assigned= $assigned; $MenuID = $_POST['menu'][$row]; $updateAddlinksYes = "UPDATE assignedlinks SET `Delete`='$v' WHERE ALID='$assigned' AND `mambo_user_id`= '$id'"; $ResultAddlinksYes = mysql_query($updateAddlinksYes, $mambo) or die(mysql_error()); echo $updateAddlinksYes; } } } // if(!empty($_POST["View"])) { while(list($k, $v) = each($_POST["View"])) { $v= "Yes"; foreach($_POST['View'] as $row=> $assigned) { $assigned= $assigned; $MenuID = $_POST['menu'][$row]; $updateAddlinksYes = "UPDATE assignedlinks SET `View`='$v' WHERE ALID='$assigned' AND `mambo_user_id`= '$id'"; $ResultAddlinksYes = mysql_query($updateAddlinksYes, $mambo) or die(mysql_error()); echo $updateAddlinksYes; } } } header("Location: index.php"); //end if submit } $colname_rsUser = "-1"; if (isset($_GET['id'])) { $colname_rsUser = (get_magic_quotes_gpc()) ? $_GET['id'] : addslashes($_GET['id']); } mysql_select_db($database_mambo, $mambo); $query_rsUser = sprintf("SELECT * FROM skills_access WHERE mambo_user_id = %s", $colname_rsUser); $rsUser = mysql_query($query_rsUser, $mambo) or die(mysql_error()); $row_rsUser = mysql_fetch_assoc($rsUser); $totalRows_rsUser = mysql_num_rows($rsUser); $id = $row_rsUser['mambo_user_id']; mysql_select_db($database_mambo, $mambo); $query_rsUpdate = "SELECT * FROM assignedlinks, menu, links WHERE assignedlinks.mambo_user_id = '$id' AND assignedlinks.MenuID = menu.MenuID AND assignedlinks.ALID = links.LinkID"; $rsUpdate = mysql_query($query_rsUpdate, $mambo) or die(mysql_error()); $row_rsUpdate = mysql_fetch_assoc($rsUpdate); $totalRows_rsUpdate = mysql_num_rows($rsUpdate); $MUID= $row_rsUpdate['MenuID']; $menuname= $row_rsUpdate['menuname']; $username= $row_rsUpdate['username']; mysql_select_db($database_mambo, $mambo); $sql= "SELECT * from assignedmenu, links WHERE mambo_user_id = '$id' AND assignedmenu.MenuID = links.MenuID AND assignedmenu.Access='Yes'"; $rssql = mysql_query($sql, $mambo) or die(mysql_error()); $row_rssql = mysql_fetch_assoc($rssql); $totalRows_rssql = mysql_num_rows($rssql); $username= $row_rssql['username']; ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Update User Permissions</title> <style type="text/css"> <!-- body,td,th { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; color: #000066; } --> </style></head> <body> <form id="update" name="update" method="post" action=""> <? if (!($_POST)){ ?> <table width="800" border="1"> <tr> <th colspan="6" valign="top" scope="row"> <div align="center"> <input name="id" type="hidden" value="<? echo $row_rsUser['mambo_user_id'];?>" /> Update User Permissions for Training Modules </div></th> </tr> <tr> <th colspan="6" valign="top" scope="row"><div align="left">Permissions will be modified for : <input name="username" type="hidden" value="<? echo $username; ?>" /> <?php echo $username;?></div> </th> </tr> <tr> <th width="172" scope="row">Menu</th> <td><strong>Assigned Links</strong></td> <td colspan="4" valign="top"><div align="center"><strong>Permissions</strong></div></td> </tr> <?php do { ?> <tr> <td scope="row"> <input name="MenuID[<? echo $row_rsUpdate['ALID'];?>]" type="hidden" value="<? echo $row_rsUpdate['MenuID'];?>" /> <? //echo $row_rsUpdate['MenuID'];?> <? echo $row_rsUpdate['menuname'] ?> </td> <td width="228"> <input name="ALID[<? echo $row_rsUpdate['ALID'];?>]" type="hidden" value="<? echo $row_rsUpdate['ALID'];?>" /> <? //echo $row_rsUpdate['ALID'];?> <? echo $row_rsUpdate['LinkName'] ?></td> <td width="102"> <?php //find the user's assigned links $rowlink=$row_rsUpdate['LinkID']; mysql_select_db($database_mambo, $mambo); $query_rsAssignedLinks = "SELECT * FROM assignedlinks WHERE username = '$username' and ALID = '$rowlink' and `Add` ='Yes'"; $rsAssignedLinks = mysql_query($query_rsAssignedLinks, $mambo) or die(mysql_error()); $row_rsAssignedLinks = mysql_fetch_assoc($rsAssignedLinks); $totalRows_rsAssignedLinks = mysql_num_rows($rsAssignedLinks); $var= $row_rsAssignedLinks['Add']; if ($var ==="Yes"){ $query_rsALFind = "UPDATE assignedlinks SET `Add`= 'No' WHERE `mambo_user_id` = '$id' AND `username` = '$username' AND ALID = '$rowlink'"; $rsALFind = mysql_query($query_rsALFind, $mambo) or die(mysql_error()); } ?> <input type="checkbox" name="Add[]" value="<? echo $row_rsUpdate['LinkID']; ?>" <?php if ($_POST && (isset($_POST['Add'])=="1")) { echo "checked=\"checked\"";} ?> <?php $OK = isset($_POST['Add']) ? true : false; if($OK && $_POST['Add']=="Yes") { ?> <? } ?>/> Add <? echo $rowlink; ?> </td> <td width="92"> <?php //find the user's assigned links $rowlink=$row_rsUpdate['LinkID']; mysql_select_db($database_mambo, $mambo); $query_rsAssignedLinks = "SELECT * FROM assignedlinks WHERE username = '$username' and ALID = '$rowlink' and `Update` ='Yes'"; $rsAssignedLinks = mysql_query($query_rsAssignedLinks, $mambo) or die(mysql_error()); $row_rsAssignedLinks = mysql_fetch_assoc($rsAssignedLinks); $totalRows_rsAssignedLinks = mysql_num_rows($rsAssignedLinks); $varUpdate= $row_rsAssignedLinks['Update']; if ($varUpdate ==="Yes"){ $query_rsALFind = "UPDATE assignedlinks SET `Update`= 'No' WHERE `mambo_user_id` = '$id' AND `username` = '$username' AND ALID = '$rowlink'"; $rsALFind = mysql_query($query_rsALFind, $mambo) or die(mysql_error()); } ?> <input type="checkbox" name="Update[]" value="<? echo $row_rsUpdate['LinkID']; ?>" <?php if ($_POST && (isset($_POST['Update'])=="1")) { echo "checked=\"checked\"";} ?> <?php $OK = isset($_POST['Update']) ? true : false; if($OK && $_POST['Update']=="Yes") { ?> <? } ?>/> Update</td> <td width="86"> <?php //find the user's assigned links $rowlink=$row_rsUpdate['LinkID']; mysql_select_db($database_mambo, $mambo); $query_rsAssignedLinks = "SELECT * FROM assignedlinks WHERE username = '$username' and ALID = '$rowlink' and `Delete` ='Yes'"; $rsAssignedLinks = mysql_query($query_rsAssignedLinks, $mambo) or die(mysql_error()); $row_rsAssignedLinks = mysql_fetch_assoc($rsAssignedLinks); $totalRows_rsAssignedLinks = mysql_num_rows($rsAssignedLinks); $varDelete= $row_rsAssignedLinks['Delete']; if ($varDelete ==="Yes"){ $query_rsALFind = "UPDATE assignedlinks SET `Delete`= 'No' WHERE `mambo_user_id` = '$id' AND `username` = '$username' AND ALID = '$rowlink'"; $rsALFind = mysql_query($query_rsALFind, $mambo) or die(mysql_error()); } ?> <input type="checkbox" name="Delete[]" value="<? echo $row_rsUpdate['LinkID']; ?>" <?php if ($_POST && (isset($_POST['Delete'])=="1")) { echo "checked=\"checked\"";} ?> <?php $OK = isset($_POST['Delete']) ? true : false; if($OK && $_POST['Delete']=="Yes") { ?> <? } ?>/> Delete</td> <td width="80"> <?php //find the user's assigned links $rowlink=$row_rsUpdate['LinkID']; mysql_select_db($database_mambo, $mambo); $query_rsAssignedLinks = "SELECT * FROM assignedlinks WHERE username = '$username' and ALID = '$rowlink' and `View` ='Yes'"; $rsAssignedLinks = mysql_query($query_rsAssignedLinks, $mambo) or die(mysql_error()); $row_rsAssignedLinks = mysql_fetch_assoc($rsAssignedLinks); $totalRows_rsAssignedLinks = mysql_num_rows($rsAssignedLinks); $varView= $row_rsAssignedLinks['View']; if ($varView ==="Yes"){ $query_rsALFind = "UPDATE assignedlinks SET `View`= 'No' WHERE `mambo_user_id` = '$id' AND `username` = '$username' AND ALID = '$rowlink'"; $rsALFind = mysql_query($query_rsALFind, $mambo) or die(mysql_error()); } ?> <input type="checkbox" name="View[]" value="<? echo $row_rsUpdate['LinkID']; ?>" <?php if ($_POST && (isset($_POST['View'])=="1")) { echo "checked=\"checked\"";} ?> <?php $OK = isset($_POST['View']) ? true : false; if($OK && $_POST['View']=="Yes") { ?> <? } ?>/>View</td> </tr> <?php } while ($row_rsUpdate = mysql_fetch_assoc($rsUpdate)); ?> <tr> <th colspan="6" scope="row"><input type="Submit" name="Submit" value="Submit" /></th> </tr> </table> <input type="hidden" name="MM_Update" value="update"> </form> <? } ?> </body> </html> <?php mysql_free_result($rsUser); mysql_free_result($rsUpdate); ?>
I keep hitting "escape", but I'm still here!!!!
:}
:}
![]() |
Other Threads in the PHP Forum
- Previous Thread: PHP tags into HTML tags
- Next Thread: Not getting information from the database
Views: 1467 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date datepart directory display download dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla jquery limit link list login loop mail menu methods mlm mod_rewrite multiple mysql oop parse password paypal pdf php problem query radio random recursion regex remote script search select seo server sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video web webdesign xml youtube






Yep. Please post your code. If anyone come across the same problem, this thread might be useful. 