Andrew Fox
Andrew Fox

Reputation: 890

How to use checkboxes to enter true (1) or false (0) into mySQL AND show as checked in php / html form?

I have a table with a field name Shift_Trig that is meant to have either a true or false value entered into it in the form of a 1 or 0. I have an html form with a checkbox for each row that, when checked, should enter a 1 into the field. I also want that checkbox to reflect the current value stored in the Shift_Trig field within the database.

The problem I'm having with my current code is whenever I UPDATE the Shift_Trig field, I'm not getting the results I want. For example, if I select:

----------------------
| ROW 1   |   false  |
----------------------
| ROW 2   |   true   |
----------------------
| ROW 3   |   false  |
----------------------
| ROW 4   |   true   |
----------------------
| ROW 5   |   true   |
----------------------

It will UPDATE like this:

----------------------
| ROW 1   |   true   |
----------------------
| ROW 2   |   true   |
----------------------
| ROW 3   |   true   |
----------------------
| ROW 4   |   false  |
----------------------
| ROW 5   |   false  |
----------------------

Here is my form code:

while($shift_query = $result_shift_query->fetch_assoc())
    {
    echo "<tr>";    
    echo "<td><input type=\"hidden\" name=\"Shift_ID[]\"></td>";
    echo "<td><input type=\"text\" name=\"Shift_Name[]\"></td>";
    echo "<td><input type=\"text\" name=\"Shift_Short_Name[]\"></td>";
    echo "<td><input type=\"text\" name=\"Shift_Color[]\"></td>";
    echo "<td><input type=\"hidden\" name=\"Shift_Trig[]\" value=\"0\"><input type=\"checkbox\" name=\"Shift_Trig[]\"";
        if($shift_query['Shift_Trig'] == '1')
            {
            echo " checked=\"checked\"";
            }
    echo " value=\"1\"></td>";
    echo "<td><input type=\"checkbox\" name=\"deleteBox[]\"></td>";
    echo "</tr>";
    }

Here is my UPDATE code:

if(isset($_POST['update_submit']))
    {
    $id = $_POST['Shift_ID'];
    $name = $_POST['Shift_Name'];
    $short_name = $_POST['Shift_Short_Name'];
    $color  = $_POST['Shift_Color'];
    $trig = $_POST['Shift_Trig'];

    $i = 0;

foreach($id as $update) 
    {
    $sql = ("UPDATE shift SET Shift_Name = '$name[$i]', Shift_Short_Name = '$short_name[$i]', Shift_Color = '$color[$i]', Shift_Trig = '$trig[$i]' WHERE Shift_ID = '$update'");
    if(!$result_shift_update = $mysqli->query($sql))
        {
        die = ("There was an error updating the shift table [" . $mysqli->error . "]");
        }
    echo "sql: " . $sql . "<br>";
    $i++;
    }

I've removed some of the code in an effort to make it easier to read, such as where it populates the text boxes with the field data from the database.

When I select rows 2 and 3 in the form, then echo out the $sql statements, I get this:

sql: UPDATE shift SET Shift_Name = 'None', Shift_Short_Name = 'None', Shift_Color = '#FF0000', Shift_Trig = '1' WHERE Shift_ID = '1'
sql: UPDATE shift SET Shift_Name = 'Morning (05:30 - 14:30)', Shift_Short_Name = 'AM', Shift_Color = '#FFF8AF', Shift_Trig = '1' WHERE Shift_ID = '2'
sql: UPDATE shift SET Shift_Name = 'Evening (14:30 - 23:30)', Shift_Short_Name = 'PM', Shift_Color = '#AF9BCF', Shift_Trig = '' WHERE Shift_ID = '3'
sql: UPDATE shift SET Shift_Name = 'General (07:30 - 17:30)', Shift_Short_Name = 'GEN', Shift_Color = '#ABFFB3', Shift_Trig = '' WHERE Shift_ID = '4'
sql: UPDATE shift SET Shift_Name = 'Night (18:00 - 06:00)', Shift_Short_Name = 'EMMA', Shift_Color = '#BFFAFF', Shift_Trig = '' WHERE Shift_ID = '5'
sql: UPDATE shift SET Shift_Name = 'Training', Shift_Short_Name = 'TRN', Shift_Color = '#FFD9BF', Shift_Trig = '' WHERE Shift_ID = '6'
sql: UPDATE shift SET Shift_Name = 'Day Off', Shift_Short_Name = 'OFF', Shift_Color = '#FFD859', Shift_Trig = '' WHERE Shift_ID = '7'

What's happening makes sense, because the first checkbox is not selected, it's not being passed to the $_POST data, but how do I write the code so I get the desired results?

EDIT : I added : <input type=\"hidden\" name=\"Shift_Trig[]\" value=\"0\"> but now I'm getting every second checkbox showing as checked.

Upvotes: 0

Views: 2022

Answers (1)

Barmar
Barmar

Reputation: 780818

You need to give the checkboxes an explicit index in the HTML:

$i = 0;
while($shift_query = $result_shift_query->fetch_assoc())
    {
    echo "<tr>";    
    echo "<td><input type=\"hidden\" name=\"Shift_ID[]\"></td>";
    echo "<td><input type=\"text\" name=\"Shift_Name[]\"></td>";
    echo "<td><input type=\"text\" name=\"Shift_Short_Name[]\"></td>";
    echo "<td><input type=\"text\" name=\"Shift_Color[]\"></td>";
    echo "<td><input type=\"checkbox\" name=\"Shift_Trig[$i]\"";
        if($shift_query['Shift_Trig'] == '1')
            {
            echo " checked=\"checked\"";
            }
    echo " value=\"1\"></td>";
    echo "<td><input type=\"checkbox\" name=\"deleteBox[$i]\"></td>";
    echo "</tr>";
    $i++;
    }

In the update code, at the beginning of the loop, do:

    if (!isset($trig[$i])) { $trig[$i] = 0; }

The rest of the update code is unchanged.

Upvotes: 1

Related Questions