0

My check boxes are created using a while loop, so i can't put id's or names on everyone of them, so i can't change the record. The check boxes are ticked if a value is 1 and not ticked if a value is 0 in my table, that's good for checking, but cant I use them to change the value of record corresponding to the check box? Here is my code:

<?php
                if($records === FALSE) { 
                    die(mysql_error());
                }

            while($student=mysql_fetch_assoc($records)){
                echo "<tr>";
                echo "<td>".$student['SID']."</td>";
                echo "<td>".$student['Student_Name']."</td>";
                if($student['Month_1'] == 1){
                echo "<td>"."<input type='checkbox' checked='true'>"."</td>";
                }else{
                    echo "<td>"."<input type='checkbox'  checked='false'>"."</td>";
                }
                if($student['Month_2'] == 1){
                echo "<td>"."<input type='checkbox' checked='true'>"."</td>";
                }else{
                    echo "<td>"."<input type='checkbox' checked='false'>"."</td>";
                }
                if($student['Month_3'] == 1){
                echo "<td>"."<input type='checkbox' checked='true'>"."</td>";
                }else{
                    echo "<td>"."<input type='checkbox' checked='false'>"."</td>";
                }
                if($student['Month_4'] == 1){
                echo "<td>"."<input type='checkbox' checked='true'>"."</td>";
                }else{
                    echo "<td>"."<input type='checkbox'  checked='false'>"."</td>";
                }
                if($student['Month_5'] == 1){
                echo "<td>"."<input type='checkbox' checked='true'>"."</td>";
                }else{
                    echo "<td>"."<input type='checkbox'  checked='false'>"."</td>";
                }
                if($student['Month_6'] == 1){
                echo "<td>"."<input type='checkbox'  checked='true'>"."</td>";
                }else{
                    echo "<td>"."<input type='checkbox' checked='false'>"."</td>";
                }
                if($student['Month_7'] == 1){
                echo "<td>"."<input type='checkbox' checked='true'>"."</td>";
                }else{
                    echo "<td>"."<input type='checkbox' checked='false'>"."</td>";
                }
                if($student['Month_8'] == 1){
                echo "<td>"."<input type='checkbox' checked='true'>"."</td>";
                }else{
                    echo "<td>"."<input type='checkbox' checked='false'>"."</td>";
                }
                if($student['Month_9'] == 1){
                echo "<td>"."<input type='checkbox' checked='true'>"."</td>";
                }else{
                    echo "<td>"."<input type='checkbox' checked='false'>"."</td>";
                }
                if($student['Month_10'] == 1){
                echo "<td>"."<input type='checkbox' checked='true'>"."</td>";
                }else{
                    echo "<td>"."<input type='checkbox' checked='false'>"."</td>";
                }
                if($student['Month_11'] == 1){
                echo "<td>"."<input type='checkbox' checked='true'>"."</td>";
                }else{
                    echo "<td>"."<input type='checkbox' checked='false'>"."</td>";
                }
                if($student['Month_12'] == 1){
                echo "<td>"."<input type='checkbox' checked='true'>"."</td>";
                }else{
                    echo "<td>"."<input type='checkbox' checked='false'>"."</td>";
                }
                echo "</tr>";
            }
        ?>
rohan510
  • 25
  • 1
  • 1
  • 10
  • 1
    well, for one, NONE of your checkboxes have `name` attributes, so there's nothing to submit if/when that form gets submitted. they also have no `value` attribute, so even if they had a name, there'd be no data to submit along with the name, so there's no way to associate any particular checkbox with any of those high-repetivive `Month_X` bits you're testing. – Marc B Apr 01 '16 at 17:13
  • maybe useful to you? [What is the proper way to check and uncheck a checkbox in HTML5?](http://stackoverflow.com/questions/12700626/what-is-the-proper-way-to-check-and-uncheck-a-checkbox-in-html5). – Ryan Vincent Apr 01 '16 at 17:59

2 Answers2

0

First, no need to write this echo "<td>"."<input type='checkbox' checked='false'>"."</td>";, just write it like this echo "<td><input type='checkbox' checked='false'></td>"; (no need for concatenation there)

Second, look this link: http://php.net/manual/en/function.mysql-fetch-assoc.php

This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.

Third, in order "to change the value of record corresponding to the check box" you basically need to send data to some script on your server ( form -> changeDbEntry.php ) and that script should connect to DB and do the work you want.

I know this isn't the answer you expect, but solution without understanding of what is going on is next to useless. So...

Advice 1: read about html forms, POST/GET requests and AJAX (later read about headers, WebSockets, protocols...)

Advice 2: Check this out: http://www.php-fig.org/psr/psr-1/

Bozidar Sikanjic
  • 717
  • 5
  • 12
0

I consider you should add a name to each checkbox containing month flag and student id like:

echo "<td>"."<input type='checkbox' checked='true' name='Month_1[".$student['SID']."]'>"."</td>";

Then after your form is submitted you can change database values in arrays with a key matching $student['SID']:

while($student=mysql_fetch_assoc($records)){
    $Month_1 = (!empty($_POST['Month_1'][$student['SID']])) ? 1 : 0; // returns 1 if checkbox was ticked or 0 if it wasn't
    // get the rest of months
    // update mysql record with `SID` = $student['SID']]
}
Seva Kalashnikov
  • 4,262
  • 2
  • 21
  • 36
  • Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in update_fees.php on line 28, do you have team viewer? it would be easier that way i guess... thanks for the help – rohan510 Apr 01 '16 at 20:42
  • What's the code on line 28? Unfortunately not able to use team viewer now – Seva Kalashnikov Apr 01 '16 at 21:03
  • $q_m1="UPDATE bio_fees SET Month_1=$Month_1 WHERE SID=$student['SID']"; – rohan510 Apr 01 '16 at 21:33
  • Try this: $q_m1="UPDATE bio_fees SET Month_1={$Month_1} WHERE SID=" . $student['SID']; – Seva Kalashnikov Apr 01 '16 at 21:46