Hi guys, wonder if any of you could figure out what am doing wrong in regards to arrays & checkboxes with the following script:

the code in question is as follows:

<?php 
function edit_st_te_event() {
		include("./error.php");
		//DB Connection
		$db = new DB_Connection;
		$db->connect();
		$database = $db->get_link_id();

	//Check if form has been submitted and update DB if it has been
	if (isset($_POST["submitted"]) &&  $_POST["submitted"] == "Update") {
		echo "<h5>FORM HAS BEEN SUBMITTED!</h5>";
		$event_id = Utility::request('event_id');
		$event = new teaching_event;
		$event->load($event_id);
		$mod_code = $event->get_module_code();
		$class_type = $event->get_class_type();

		//The form was submitted, now check $_POST['check_1']
        if (!(empty($_POST['check_1']))) { 
			echo "<h5>CHECK_1 IS NOT EMPTY!</h5>";
			foreach ($_POST['check_1'] AS $id) {
				$query = "UPDATE st_te_event SET";

				for ($i = 1; $i <= 12; $i++) {
        			$val = $_POST["check_$i"];
        			if (!$val) { 
						$val = 0;
					} //close if
        			$query.= " Week$i = $val,";
    			} //close for

        		$query = substr($query, 0, -1); // Trim trailing comma
        		$query.= " WHERE student_id = '$id' AND module_code = '$mod_code' AND class_type = '$class_type'";

    			echo "Update query is : <p>$query</p>";
        		if (!mysql_query ($query,$database)) { 
    				echo mysql_error();
    			} //close if
			} //close foreach 
		} //close if
	} //close if

	$event_id = Utility::request('event_id');
	$event = new teaching_event;
	$event->load($event_id);

	//capture form variables
	$year = $event->get_year();
	$group_no = $event->get_group_no();
	$mod_code = $event->get_module_code();
	$class_type = $event->get_class_type();

	?>
	<!---------------------Display the Table-------------------->
	<table border=1 cellspacing="2" cellpadding="4" width="100%" style="background-color:#EEE">
	<tr><th>Year</th><th>Group</th><th>Module</th><th>Class</th><th>Student ID</th><th>Name</th>
	<?php
	for ($i=1; $i <= 12; $i++) {
		echo "<th>Wk{$i}</th>";
	} //close for
	echo "<th>Total</th>";
	echo "</tr>";

	//Load all of the student records from the st_te_event table
	$st_te_event = new st_te_event;
	$teaching_events = st_te_event::get_selected($year, $group_no, $mod_code, $class_type);
	$num = count($teaching_events);
	if ($num == 0) {
		echo "<center><h5><font color='#000099'>The selected teaching event has not yet been setup!</font></h5></center>";
		echo "<META HTTP-EQUIV='refresh' content='3;URL=./index.php?mode=view&section=new_teaching_event'>";
	} //close if
	else {
		for ($count = 0; $count < $num; $count++) {
			$record = $teaching_events[$count];
			$year = $record->get_year();
			$group_no = $record->get_group_no();
			$mod = $record->get_module_code();
			$class = $record->get_class_type();
			$id = $record->get_student_id();
			$name = $record->get_name();

			echo "<tr>";
    		echo "<form name='form1' method='post' action='./index.php?mode=view&section=edit_st_te_event'>";
    		echo "<td>$year</td>";
			echo "<td>$group_no</td>";
			echo "<td>$mod</td>";
    		echo "<td>$class</td>";
			echo "<td>$id</td>";
			echo "<td>$name</td>";

			$total = 0;
			$percent = 0;

    		//Create the 12 checkboxes for each week of the student's attendance'
			for ($i = 1; $i <= 12; $i++) {
				$str1 = "get_week";
				$str2 = "$i";
				$str3 = "()";
				$exp = $str1.$str2.$str3;
				$val = $record->$exp;
        		echo "<td>";
				$s1 = "check_";
				$s2 = "$i";
				$s3 = "[]";
				$check = $s1.$s2.$s3;
        		echo "<input type='checkbox' name='$check' value='$id'";
        		if ($val) {
					echo " CHECKED";
					echo ">";
				} //close if
        		echo "</td>";

				//increment total
				if ($val) { 
					$total++;
				} //close if
    		} //close for

			//Caclulate total as percentage
			$percent = round(($total / 12) * 100);

			echo "<td>$percent%</td>";
			echo "<input type='hidden' name='event_id' value='$event_id'>";
    		echo "</tr>";

			//Insert total as percentage to database
			$sql = "UPDATE st_te_event SET Total = '$percent' WHERE student_id = '$id' AND module_code = '$mod_code' AND class_type = '$class_type'";
			$sql_result = mysql_query($sql, $database) or die (mysql_error());
			unset($record);
		} //close for

		echo "
		<tr>
			<td>
			<input type='submit' name='submitted' value='Update'>
			</td>
		</tr>
		</form>";
		mysql_close($database);
		?>
		</table>
		<br>
		<?php
	} //close else
} //close edit_st_te_event

Basically what I want to be able to do is check the checkboxs for each students attendance, and update each record in the st_te_event table just by hitting the one "Update" button at the bottom of the page.

The code above uses an array of checkboxes which record the student ID, however how can I record the student ID as well as their 12 weeks attendance records?

to see what I am trying to achieve with the script above goto HERE! and for login use:

user - ocuk
pass - ocuk

Once logged in if you goto the heading "Edit/Add Lecture" on the left hand nav bar and under that click "Edit Lecture" this will display a test lecture which ive setup, click on "Click to edit student attendances" and it will load the script I am having problems with above.

What I want to achieve is a way to check whether a student attended a particular weeks lecture and do this for all students on the table then hit the one update button at the bottom to update the database.

I would appreciate any help or advice any of you can give me on this, at the moment when I select the checkboxes for several students then hit "Update" I get the following error:

Update query is :
UPDATE st_te_event SET Week1 = Array, Week2 = Array, Week3 = Array, Week4 = 0, Week5 = 0, Week6 = 0, Week7 = 0, Week8 = 0, Week9 = 0, Week10 = 0, Week11 = 0, Week12 = 0 WHERE student_id = '10027700' AND module_code = 'ACF504J1' AND class_type = 'Lecture (2 hour)'

Unknown column 'Array' in 'field list'

cheers!

    Backtracking through your code to find out where the Arrays are coming from eventually leads me to this:

    $val = $_POST["check_$i"]; 

    My guess is that $_POST["check_1"] and so on are arrays.

      You may be doing this a really difficult way... as Weed said... your trying to insert Arrays.... and the way you are storing them will make it difficult to get individual student data into 1 line to update the database with... currently how you have it...

      check_1[0] = student 1 week 1
      check_2[0] = student 1 week 2
      check_3[0] = student 1 week 3
      check_1[5] = student 6 week 1
      check_5[2] = student 3 week 2...

      would be simpler... if you rewrote the page to build the form as...

      student_1[]
      student_2[]

      then you can access the data in a manner like...
      student_1[0] = student 1 week 1
      student_2[3] = student 2 week 4

      if you have to use it in the manner you currently have it... will take some creative looping to build your UPDATE query... atleast from my standpoint right now...

        Originally posted by Weedpacket
        Backtracking through your code to find out where the Arrays are coming from eventually leads me to this:

        $val = $_POST["check_$i"]; 

        My guess is that $_POST["check_1"] and so on are arrays. [/B]

        Hi yes, that is an array, what am trying to figure out is how I can obtain the value for each of a student's 12 weeks attendances. from the array so that I can update his/her record in the database as week1, week2, week3 etc to week12.

        I have also just noticed that the code:

        $val = $record->$exp;
        

        Doesn't work, what I am trying to achieve here is calling the get_week1()
        get_week2()
        get_week3()
        get_week4()
        get_week5()
        get_week6()
        get_week7()
        get_week8()
        get_week9()
        get_week10()
        get_week11()
        get_week12()

        methods, but using the counter in the for loop $i.

        Is there a way I can concatinate the expression to get something that reads:

        $val = $record->get_week$i();
        

        Thanks for the help.

          Originally posted by tekky
          You may be doing this a really difficult way... as Weed said... your trying to insert Arrays.... and the way you are storing them will make it difficult to get individual student data into 1 line to update the database with... currently how you have it...

          check_1[0] = student 1 week 1
          check_2[0] = student 1 week 2
          check_3[0] = student 1 week 3
          check_1[5] = student 6 week 1
          check_5[2] = student 3 week 2...

          would be simpler... if you rewrote the page to build the form as...

          student_1[]
          student_2[]

          then you can access the data in a manner like...
          student_1[0] = student 1 week 1
          student_2[3] = student 2 week 4

          if you have to use it in the manner you currently have it... will take some creative looping to build your UPDATE query... atleast from my standpoint right now...

          Hi mate, can you show me a quick example of what you mean in my code above, say for the first 2 or 3 students, then I can see what way you are explaining it. I know the way I have been trying it is difficult, but its the only way I thought it would work and yes I would require some very nice loops to get the update to work.

          cheers!

            I think that's because You didn't use the index correct. Try creating the array with check[11] instead of check_11[].

              Hi tekky, I am not quite sure how you are explaining to me how to re-build the script the new way you are talking about.

              Could you show me a quick example from a bit of my code above to see what you mean?

              cheers!

                Dont count on this working first try.... but I took a shot at rewriting it to work in a manner I might use...

                note: i dont use the same bracket method you use, its in my format so I could track them easier.... hope it works... or atleast gives you an idea of what I was trying to explain the other night

                -Karl

                <?php 
                function edit_st_te_event()
                { 
                	include("./error.php"); 
                	//DB Connection 
                	$db = new DB_Connection; 
                	$db->connect(); 
                	$database = $db->get_link_id(); 
                
                //Check if form has been submitted and update DB if it has been 
                if (isset($_POST["submitted"]) && $_POST["submitted"] == "Update")
                { 
                	echo "<h5>FORM HAS BEEN SUBMITTED!</h5>"; 
                	$event_id = Utility::request('event_id'); 
                	$event = new teaching_event; 
                	$event->load($event_id); 
                	$mod_code = $event->get_module_code(); 
                	$class_type = $event->get_class_type(); 
                
                	//The form was submitted, now check $_POST['check_1'] 
                	if (!(empty($_POST['class'])))
                	{ 
                		echo "<h5>CLASS IS NOT EMPTY!</h5>"; 
                		foreach ($_POST['class'] AS $class)
                		{ 
                			$query = "UPDATE st_te_event SET"; 
                
                			foreach $class as $student
                			{ 
                				foreach $student as $week
                				{
                					$val = $week; 
                					if (!$val) { $val = 0; }
                					$query.= " Week$i = $val,"; 
                				} //close foreach week
                				$query = substr($query, 0, -1); // Trim trailing comma 
                				$query.= " WHERE student_id = '$id' AND module_code = '$mod_code' AND class_type = '$class_type'"; 
                
                				echo "Update query is : <p>$query</p>"; 
                				if (!mysql_query ($query,$database))
                				{ 
                					echo mysql_error(); 
                				} //close if 
                			} //close foreach student
                		} //close foreach class
                	} //close if 
                } //close if 
                
                $event_id = Utility::request('event_id'); 
                $event = new teaching_event; 
                $event->load($event_id); 
                
                //capture form variables 
                $year = $event->get_year(); 
                $group_no = $event->get_group_no(); 
                $mod_code = $event->get_module_code(); 
                $class_type = $event->get_class_type(); 
                
                ?> 
                <!---------------------Display the Table--------------------> 
                <table border=1 cellspacing="2" cellpadding="4" width="100%" style="background-color:#EEE"> 
                <tr><th>Year</th><th>Group</th><th>Module</th><th>Class</th><th>Student ID</th><th>Name</th> 
                <?php 
                	for ($i=1; $i <= 12; $i++)
                	{ 
                		echo "<th>Wk{$i}</th>"; 
                	} //close for 
                	echo "<th>Total</th>"; 
                	echo "</tr>"; 
                
                //Load all of the student records from the st_te_event table 
                $st_te_event = new st_te_event; 
                $teaching_events = st_te_event::get_selected($year, $group_no, $mod_code, $class_type); 
                $num = count($teaching_events); 
                if ($num == 0)
                { 
                	echo "<center><h5><font color='#000099'>The selected teaching event has not yet been setup!</font></h5></center>"; 
                	echo "<META HTTP-EQUIV='refresh' content='3;URL=./index.php?mode=view&section=new_teaching_event'>"; 
                } //close if 
                else
                { 
                	$curStudent = 0;
                	for ($count = 0; $count < $num; $count++)
                	{ 
                		$record = $teaching_events[$count]; 
                		$year = $record->get_year(); 
                		$group_no = $record->get_group_no(); 
                		$mod = $record->get_module_code(); 
                		$class = $record->get_class_type(); 
                		$id = $record->get_student_id(); 
                		$name = $record->get_name(); 
                
                		echo "<tr>"; 
                		echo "<form name='form1' method='post' action='./index.php?mode=view&section=edit_st_te_event'>"; 
                		echo "<td>$year</td>"; 
                		echo "<td>$group_no</td>"; 
                		echo "<td>$mod</td>"; 
                		echo "<td>$class</td>"; 
                		echo "<td>$id</td>"; 
                		echo "<td>$name</td>"; 
                
                		$total = 0; 
                		$percent = 0; 
                
                		$curStudent++;
                		//Create the 12 checkboxes for each week of the student's attendance'
                		for ($i = 1; $i <= 12; $i++)
                		{
                			$exp = "get_week$i()";
                			$val = $record->$exp;
                			echo "<td>";
                			$check = "class[$mod][$curStudent][]";
                			echo "<input type='checkbox' name='$check' value='$id'";
                			if ($val)
                			{
                		    		echo " CHECKED";
                				echo ">";
                			} //close if
                			echo "</td>";
                			//increment total
                			if ($val)
                			{ 
                				$total++;
                			} //close if
                		} //close for
                
                		//Caclulate total as percentage
                		$percent = round(($total / 12) * 100);
                		echo "<td>$percent%</td>";
                		echo "<input type='hidden' name='event_id' value='$event_id'>";
                		echo "</tr>";
                
                		//Insert total as percentage to database
                		$sql = "UPDATE st_te_event SET Total = '$percent' WHERE student_id = '$id' AND module_code = '$mod_code' AND class_type = '$class_type'";
                		$sql_result = mysql_query($sql, $database) or die (mysql_error());
                		unset($record);
                	} //close for	
                	echo "<tr><td><input type='submit' name='submitted' value='Update'></td></tr></form>"; 
                	mysql_close($database); 
                ?> 
                </table> 
                <br> 
                <?php 
                	} //close else 
                } //close edit_st_te_event
                

                  Hi tekky, thanks very much for the help, its most appreciated. I copied the changes that you made to the script over, however the code is not picking up on whether weeks 1 - 12 for each student are set to 1 or 0 (attended or not attended) so at the moment the database is trying to update each week number with the student's id instead of the values 1 or 0.

                  I also added the changes of $i to the second foreach so that each week was getting refered to as week1, week2 etc and not just week.

                  //Check if form has been submitted and update DB if it has been 
                          if (isset($_POST["submitted"]) && $_POST["submitted"] == "Update") {  
                  echo "<h5>FORM HAS BEEN SUBMITTED!</h5>";
                  $event_id = Utility::request('event_id');
                  $event = new teaching_event;
                  $event->load($event_id);
                  $mod_code = $event->get_module_code();
                  $class_type = $event->get_class_type(); //The form was submitted, now check $_POST['check_1'] if (!(empty($_POST['class']))) { echo "<h5>CLASS IS NOT EMPTY!</h5>"; foreach ($_POST['class'] AS $class) { $query = "UPDATE st_te_event SET"; foreach ($class as $student) { $i = 1; foreach ($student as $week) { $val = $week; if (!$val) { $val = 0; } //close if $query.= " Week$i = $val,"; $i++; //increment i } //close foreach week $query = substr($query, 0, -1); // Trim trailing comma $query.= " WHERE student_id = '$week' AND module_code = '$mod_code' AND class_type = '$class_type'"; echo "Update query is : <p>$query</p>"; if (!mysql_query ($query,$database)) { echo mysql_error(); } //close if } //close foreach student } //close foreach class } //close if } //close if

                  This is just the submit part of the script.

                  If I check a few checkboxes for each student and hit update I get the following output from the query and error:

                  FORM HAS BEEN SUBMITTED!
                  CLASS IS NOT EMPTY!
                  Update query is :
                  UPDATE st_te_event SET Week1 = 10027700, Week2 = 10027700, Week3 = 10027700 WHERE student_id = '10027700' AND module_code = 'ACF504J1' AND class_type = 'Lecture (2 hour)'

                  Update query is :
                  UPDATE st_te_event SET Week1 = 10027700, Week2 = 10027700, Week3 = 10027700 WHERE student_id = '10027700' AND module_code = 'ACF504J1' AND class_type = 'Lecture (2 hour)' Week1 = 11072101, Week2 = 11072101, Week3 = 11072101 WHERE student_id = '11072101' AND module_code = 'ACF504J1' AND class_type = 'Lecture (2 hour)'

                  You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Week1 = 11072101, Week2 = 11072101, Week3 = 11072101 WHERE studUpdate query is :
                  UPDATE st_te_event SET Week1 = 10027700, Week2 = 10027700, Week3 = 10027700 WHERE student_id = '10027700' AND module_code = 'ACF504J1' AND class_type = 'Lecture (2 hour)' Week1 = 11072101, Week2 = 11072101, Week3 = 11072101 WHERE student_id = '11072101' AND module_code = 'ACF504J1' AND class_type = 'Lecture (2 hour)' Week1 = 11074801, Week2 = 11074801, Week3 = 11074801 WHERE student_id = '11074801' AND module_code = 'ACF504J1' AND class_type = 'Lecture (2 hour)'

                  You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Week1 = 11072101, Week2 = 11072101, Week3 = 11072101 WHERE studUpdate query is :
                  UPDATE st_te_event SET Week1 = 10027700, Week2 = 10027700, Week3 = 10027700 WHERE student_id = '10027700' AND module_code = 'ACF504J1' AND class_type = 'Lecture (2 hour)' Week1 = 11072101, Week2 = 11072101, Week3 = 11072101 WHERE student_id = '11072101' AND module_code = 'ACF504J1' AND class_type = 'Lecture (2 hour)' Week1 = 11074801, Week2 = 11074801, Week3 = 11074801 WHERE student_id = '11074801' AND module_code = 'ACF504J1' AND class_type = 'Lecture (2 hour)' Week1 = 11076401, Week2 = 11076401, Week3 = 11076401, Week4 = 11076401, Week5 = 11076401 WHERE student_id = '11076401' AND module_code = 'ACF504J1' AND class_type = 'Lecture (2 hour)'

                  You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Week1 = 11072101, Week2 = 11072101, Week3 = 11072101 WHERE studUpdate query is :
                  UPDATE st_te_event SET Week1 = 10027700, Week2 = 10027700, Week3 = 10027700 WHERE student_id = '10027700' AND module_code = 'ACF504J1' AND class_type = 'Lecture (2 hour)' Week1 = 11072101, Week2 = 11072101, Week3 = 11072101 WHERE student_id = '11072101' AND module_code = 'ACF504J1' AND class_type = 'Lecture (2 hour)' Week1 = 11074801, Week2 = 11074801, Week3 = 11074801 WHERE student_id = '11074801' AND module_code = 'ACF504J1' AND class_type = 'Lecture (2 hour)' Week1 = 11076401, Week2 = 11076401, Week3 = 11076401, Week4 = 11076401, Week5 = 11076401 WHERE student_id = '11076401' AND module_code = 'ACF504J1' AND class_type = 'Lecture (2 hour)' Week1 = 11828501, Week2 = 11828501, Week3 = 11828501, Week4 = 11828501 WHERE student_id = '11828501' AND module_code = 'ACF504J1' AND class_type = 'Lecture (2 hour)'

                  You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Week1 = 11072101, Week2 = 11072101, Week3 = 11072101 WHERE stud

                  Thats what I get when I tick the first 4 checkboxes for the first 5 students.

                  I have uploaded the new script it can be viewed HERE!

                  For Login use:
                  user - ocuk
                  pass - ocuk

                  Once logged in if you goto the heading "Edit/Add Lecture" on the left hand nav bar and under that click "Edit Lecture" this will display a test lecture which ive setup, click on "Click to edit student attendances" and it will load the script I am having problems with above.

                  Any help is appreciated, thanks guys!

                    the page you said had the new code, appears to not have the new code.... still seeing check_xx[] for the check boxes...

                    however, you could try updating this....

                    if (!$val) {  
                    $val = 0;
                    } //close if //to this... if (!$val) { $val = 0; } else { $val = 1; }

                    also may want to throw a vardump $POST at the top of the apge so you can see what the $POST vars look like for sure...

                      Ok I have made that change, and I am pleased to say that the following script now does what I want it to, that is update each students week which has been checked with a value = '1'.

                      <?php
                      function edit_st_te_event() {  
                      include("./error.php");
                      //DB Connection $db = new DB_Connection;
                      $db->connect();
                      $database = $db->get_link_id(); //Check if form has been submitted and update DB if it has been if (isset($_POST["submitted"]) && $_POST["submitted"] == "Update") { echo "<h5>FORM HAS BEEN SUBMITTED!</h5>"; $event_id = Utility::request('event_id'); $event = new teaching_event; $event->load($event_id); $mod_code = $event->get_module_code(); $class_type = $event->get_class_type(); //The form was submitted, now check $_POST['check_1'] if (!(empty($_POST['class']))) { echo "<h5>CLASS IS NOT EMPTY!</h5>"; foreach ($_POST['class'] AS $class) { //$query = "UPDATE st_te_event SET"; foreach ($class as $student) { $query = "UPDATE st_te_event SET"; $i = 1; foreach ($student as $week) { $val = $week; if (!$val) { $val = 0; } //close if else { $val = 1; } //close else $query.= " Week$i = $val,"; $i++; //increment i } //close foreach week $query = substr($query, 0, -1); // Trim trailing comma $query.= " WHERE student_id = '$week' AND module_code = '$mod_code' AND class_type = '$class_type'"; echo "Update query is : <p>$query</p>"; if (!mysql_query ($query,$database)) { echo mysql_error(); } //close if } //close foreach student } //close foreach class } //close if } //close if $event_id = Utility::request('event_id'); $event = new teaching_event; $event->load($event_id); //capture form variables $year = $event->get_year(); $group_no = $event->get_group_no(); $mod_code = $event->get_module_code(); $class_type = $event->get_class_type(); ?> <!---------------------Display the Table--------------------> <table border="0" cellspacing="2" cellpadding="4" width="100%" style="background-color:#EEE"> <tr bgcolor="#A8C0D9"><th>Year</th><th>Group</th><th>Module</th><th>Class</th><th>Student ID</th><th>Name</th> <?php for ($i=1; $i <= 12; $i++) { echo "<th>Wk{$i}</th>"; } //close for echo "<th>Total</th>"; echo "</tr>"; //Load all of the student records from the st_te_event table $st_te_event = new st_te_event; $teaching_events = st_te_event::get_selected($year, $group_no, $mod_code, $class_type); $num = count($teaching_events); if ($num == 0) { echo "<center><h5><font color='#000099'>The selected teaching event has not yet been setup!</font></h5></center>"; echo "<META HTTP-EQUIV='refresh' content='3;URL=./index.php?mode=view&section=new_teaching_event'>"; } //close if else { $curStudent = 0; $alternate = 2; //Colour of row; for ($count = 0; $count < $num; $count++) { $record = $teaching_events[$count]; $year = $record->get_year(); $group_no = $record->get_group_no(); $mod = $record->get_module_code(); $class = $record->get_class_type(); $id = $record->get_student_id(); $name = $record->get_name(); // row colours if ($alternate == 1) { $colour = "#ffffff"; $alternate = 2; } //close if else { $colour = "#efefef"; $alternate = 1; } //close else echo "<tr bgcolor=".$colour.">"; echo "<form name='form1' method='post' action='./index.php?mode=view&section=edit_st_te_event'>"; echo "<td>$year</td>"; echo "<td>$group_no</td>"; echo "<td>$mod</td>"; echo "<td>$class</td>"; echo "<td>$id</td>"; echo "<td>$name</td>"; $total = 0; $percent = 0; $curStudent++; //Create the 12 checkboxes for each week of the student's attendance' for ($i = 1; $i <= 12; $i++) { $val = $record->get_week($i); echo "<td>"; $check = "class[$mod][$curStudent][]"; echo "<input type='checkbox' name='class1[]' value='$id'"; if ($val) { echo " CHECKED"; echo ">"; $total++; //increment total } //close if echo "</td>"; } //close for //Caclulate total as percentage $percent = round(($total / 12) * 100); echo "<td>$percent%</td>"; echo "<input type='hidden' name='event_id' value='$event_id'>"; echo "</tr>"; //Insert total as percentage to database $sql = "UPDATE st_te_event SET Total = '$percent' WHERE student_id = '$id' AND module_code = '$mod_code' AND class_type = '$class_type'"; $sql_result = mysql_query($sql, $database) or die (mysql_error()); unset($record); } //close for echo "<tr><td><input type='submit' name='submitted' value='Update'></td></tr></form>"; mysql_close($database); ?> </table> <br> <?php } //close else } //close edit_st_te_event

                      My only problem now is that if a mistake is made and a students attendance is recorded wrongly, the current script is unable to reset a previously set value of '1' back to '0' if the checkbox is unchecked, is there any way around this?

                      Thanks once again for all the help, ive been pulling my hair out over this thing all week.

                      cheers!

                        put a print_r($POST); at the beginning of the database update section so we can see how the $POST variable is getting defined with the non checked boxes.... then we can go from there

                          $_POST doesn't POST empty checkboxes.

                          Suppose you have only one input in a form:
                          <input type=checkbox name=checkme value=1 checked>

                          If checked
                          $_POST['checkme']=1;

                          If unchecked
                          Nothing will be $_POSTed

                          Work around is:

                          if($_POST['checkme']){$mycheckbox=1;}
                          else{$mycheckbox=0;}

                          INSERT INTO mytable SET mycolumn=$mycheckbox

                            Ok guys, have uploaded the new script to the server above, have also put the print_r($_POST) at the top of the form, so it prints the values when the update button is pressed.

                            nemonoman how can I use that method you described for empty checkboxes in my code to update the database with the value of 0 if the checkbox which has previously been checked is unchecked and the update button is clicked?

                            Thanks for the help guys.

                              this may work also....

                              					foreach $student as $week
                              					{ 
                              						if (empty($week)) || (!isset($week)) { $val = 0; }
                              						else { $val = 1; }
                              						$query.= " Week$i = $val,"; 
                              					} //close foreach week
                              

                                Originally posted by tekky
                                this may work also....

                                					foreach $student as $week
                                					{ 
                                						if (empty($week)) || (!isset($week)) { $val = 0; }
                                						else { $val = 1; }
                                						$query.= " Week$i = $val,"; 
                                					} //close foreach week
                                

                                [/B]

                                Tried this, and it gives the same as before, but when a checkbox is unchecked its still not setting the value back to 0. Also when the form reloads and I uncheck a box then hit update, the script isn't running because there are no values held in $_POST[check], so its not entering into that loop.

                                so close 🙂

                                thanks guys!!

                                  maybe this would work....

                                  in the bottom half... change...

                                  $check = "class[$mod][$curStudent][]";
                                  
                                  //to this...
                                  $check = "class[$mod][$curStudent][$i-1]";
                                  

                                  in the top half... change

                                  foreach $student as $week
                                  					{
                                  						$val = $week; 
                                  						if (!$val) { $val = 0; }
                                  						$query.= " Week$i = $val,"; 
                                  					} //close foreach week
                                  
                                  //to this....
                                  for($stu=0;$stu<13;$stu++)					{
                                  						$val = $student[$stu]; 
                                  						if (!$val) { $val = 0; }
                                  						$query.= " Week$i = $val,"; 
                                  					} //close for
                                  

                                    Ok tekky I gave that a try, when I load the page and uncheck a previously checked checkbox, the Update Query is:

                                    Update query is :
                                    UPDATE st_te_event SET Week1 = 0, Week2 = 0, Week3 = 0, Week4 = 0, Week5 = 0, Week6 = 0, Week7 = 0, Week8 = 0, Week9 = 0, Week10 = 0, Week11 = 0, Week12 = 0, Week13 = 0 WHERE student_id = '' AND module_code = 'ACF504J1' AND class_type = 'Lecture (2 hour)'

                                    Unknown column 'Week13' in 'field list'

                                    So the script isn't picking up the studentid and is setting all values to 0.

                                    ill have another tinker and see if I can sort this.

                                    cheers!

                                      Write a Reply...