doulang2311 2012-12-23 10:42
浏览 56
已采纳

如何使用复选框在mySQL中输入true(1)或false(0)并以php / html格式显示?

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条回答 默认 最新

  • dongxing4805 2012-12-23 10:47
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路