PHP help with processing multiple checkboxes

howdy all … my first post on here. Thanks in advance!

I am having trouble with processing multiple checkboxes that are created dynamically and processed dynamically.

The view and submit are in the same file as you will see.

Use the form for a little bit and see the issue. If you check multiple people and save, then go back and uncheck someone and save, go back and the other people are unchecked and the person who you just unchecked is checked … if that made any sense.

Take a look.

link is http://69.175.21.50/~stbriga1/app/attendance/

[php]

<?php $docroot = "../"; include($docroot.'inc/functions.inc.php'); $cmd = $_GET['cmd']; $id = $_GET['id']; if (isset($cmd)) {} else { $cmd = 'view'; } switch ($cmd) { case 'view': do_view(); break; case 'view_attendance': do_view_attendance($id); break; case 'submit': do_submit($id); break; } // ################################################################################################# function do_view() { $docroot = "../"; dbConnect(); // top navigation location, // Values: Home, Admin, Members, Attendance, Finance, Friends, Instruments, Equipment, Music Library, Products, Jobs $current_menunav = "Attendance"; // left nav bar location, //Values: Welcome, Support, Add, List, Attendance, Scriptures $current_leftnav = "Home"; site_header("Home", $docroot, $current_menunav); $sql = "SELECT * FROM `_events`"; $result = mysql_query($sql) or perish ("Unable to execute SQL query on database ::
\r\n" . mysql_error()); ?>
	<h3>Attendance to Events</h3><br/>
	<div id="container">
		<div id="demo"> 
		
			<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"> 
				<thead> 
					<tr> 
						<th>&nbsp;</th>
						<th>&nbsp;</th> 
						<th>&nbsp;</th>
						<th>&nbsp;</th>
						<th>&nbsp;</th>
						<th>&nbsp;</th>
						<th>&nbsp;</th>
					</tr> 
				</thead> 
				<tbody> 
<?php $i=1; while ($row = mysql_fetch_array($result)) { if ($i % 2) { $css = "odd_gradeX";} else { $css = "even_gradeC";} echo ''; echo ''; if ($row['start_date'] != $row['end_date']) { echo date("m/d/Y", strtotime($row['start_date'])).' - '.date("m/d/Y", strtotime($row['end_date'])); } else { echo date("m/d/Y", strtotime($row['start_date'])); } echo ''; echo ''.date("D", strtotime($row['start_date'])).''; echo ''; if ($row['status'] == 5) { echo ''.$row['name'].''; } else { echo $row['name']; } echo ''; echo ''.$row['city'].', '.$row['state'].''; echo ''.get_time($row['time']).''; echo ''.get_proper_name($row['status'], "status", "_events_status").''; //get_proper_name($val, $fld, $tbl) echo ''; echo ""; $i = ++$i; } ?>
				</tbody> 
			</table> 
		</div> 
	</div> 
<?php site_footer($docroot, $current_menunav, $current_leftnav); } // ################################################################################################# function do_view_attendance($id) { $id = $_GET['id']; $docroot = '../'; dbConnect(); // Connect to the DB $sqlevents = "SELECT * FROM `_events` WHERE id = '".$id."'"; $resultevents = mysql_query($sqlevents) or perish ("Unable to execute SQL query on database ::
\r\n" . mysql_error()); $sqlinstruments = "SELECT * FROM `instrument_names`"; $resultinstruments = mysql_query($sqlinstruments) or perish ("Unable to execute SQL query on database ::
\r\n" . mysql_error()); // change to handle more delicately... die for now if ($rowevents = mysql_fetch_array($resultevents)) { // top navigation location, // Values: Home, Admin, Members, Attendance, Finance, Friends, Instruments, Equipment, Music Library, Products, Jobs $current_menunav = "Attendance"; // left nav bar location, //Values: Welcome, Support, Add, List, Attendance, Scriptures $current_leftnav = "Home"; site_header("Home", $docroot, $current_menunav); } else { // bogus id sent? can't be editing... exit; } echo '

Event : '.$rowevents['name'].' at '.$rowevents['place_name'].'

'; ?>
<?php while ($rowinstruments = mysql_fetch_array($resultinstruments)) { echo ' '; $sqlmembers = "SELECT id, fn, ln, primary_instrument FROM `members` WHERE `primary_instrument` = '".$rowinstruments['id']."' ORDER BY `ln`"; $resultmembers = mysql_query($sqlmembers) or perish ("Unable to execute SQL query on database ::
\r\n" . mysql_error()); while ($rowmembers = mysql_fetch_array($resultmembers)) { if ($rowinstruments['id'] == $rowmembers['primary_instrument']) { echo ' '; } } } echo ' '; echo '

Here ?
'.$rowinstruments['name'].'
  '.$rowmembers['fn'].' '.$rowmembers['ln'].' <input type="checkbox" name="value[]" value="'.$rowmembers['id'].'"'; $sqlattendance = "SELECT * from `attendance` WHERE `member_id` = '".$rowmembers['id']."' AND `event_id` = '".$rowevents['id']."'"; $resultsattendance = mysql_query($sqlattendance) or perish ("Unable to execute SQL query on database ::
\r\n" . mysql_error()); if ($result = mysql_fetch_array($resultsattendance)) { echo ' checked';} echo '>

'; site_footer($docroot, $current_menunav, $current_leftnav); } // ################################################################################################# function do_submit($id) { $id = $_GET['id']; $docroot = "../"; dbConnect(); if ($_POST['cmd'] == "Cancel") { redirect('index.php', 'Attendance index', 'Action cancelled. Returning to main menu.', $debug ? 600 : 2); // redirect back to list exit; } else { $date = date("Y-m-d"); $aValue = $_POST['value']; if(empty($aValue)) { mysql_query($sql = "DELETE FROM `attendance` WHERE `event_id` = '".$id."'") or perish(mysql_error()); redirect('index.php', 'Attendance Index', '0 attendance records entered. Returning to main menu.', $debug ? 600 : 2); exit(); } else { $N = count($aValue); for($i=0; $i < $N; $i++) { $sqlattendance = "SELECT * FROM `attendance` WHERE member_id = '".$aValue[$i]."' AND event_id = '".$id."'"; $resultsattendance = mysql_query($sqlattendance) or perish ("Unable to execute SQL query on database ::
\r\n" . mysql_error()); if ($result = mysql_fetch_array($resultsattendance)) { $sqlcheck = "SELECT `id` FROM `attendance` WHERE event_id = '".$id."'"; $resultcheck = mysql_query($sqlcheck) or perish ("Unable to execute SQL query on database ::
\r\n" . mysql_error()); while ($rowcheck = mysql_fetch_array($resultcheck)) { if ( $result['id'] != $resultcheck['id'] ) { mysql_query($sql = "DELETE FROM `attendance` WHERE `id` = '".$result['id']."'") or perish(mysql_error()); } else { $sql = "INSERT INTO `attendance` ( `member_id`, `event_id`, `date_entered` ) VALUES ( '".$aValue[$i]."', '".$id."', '".$date."' )"; mysql_query($sql) or perish($sql."
\r\n" . mysql_error()); } } } else { $sql = "INSERT INTO `attendance` ( `member_id`, `event_id`, `date_entered` ) VALUES ( '".$aValue[$i]."', '".$id."', '".$date."' )"; mysql_query($sql) or perish($sql."
\r\n" . mysql_error()); } } } } redirect('index.php', 'Attendance Index', ''.$N.' attendance records entered. Returning to main menu.', $debug ? 600 : 2); exit(); } ?>

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service