Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9030881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:35:58+00:00 2026-06-16T07:35:58+00:00

I have a table with a field name Shift_Trig that is meant to have

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:36:00+00:00Added an answer on June 16, 2026 at 7:36 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in Mysql (firstname,lastname,data1,data2,...) that one field name is MYDATE and
Hi guys I have Table Item that have a Name field. I am trying
i have table called as Support, which have a field named Name and contains
I have a looklup table, its got an ID and a NAME field. The
i have some fields in my database table,and a field with phone name ,
I have table Field with : t.string name t.integer id_survey t.integer id_participant and I
I have field name category_id in product table. And also I want name field
I have a table with a field name BID with its data type set
I have a table with one field as name.The values in this fields are
i have table name is ads like that: id ad_country ad_gender ad_birthday 1 05

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.