如何使用php创建的复选框更改数据库中的记录


How can I change a record in a database with a checkbox created with php

我的复选框是使用while循环创建的,所以我不能把id或名称放在每个复选框上,所以我无法更改记录。在我的表中,如果值为1,复选框会被勾选;如果值为0,复选框不会被勾选,这很适合检查,但我不能用它们来更改与复选框对应的记录的值吗?这是我的代码:

<?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>";
            }
        ?>

我认为您应该为每个包含月份标志和学生id的复选框添加一个名称,如:

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

然后,在您的表格提交后,您可以使用匹配$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']]
}

首先,不需要写这个echo "<td>"."<input type='checkbox' checked='false'>"."</td>";,只需要像写echo "<td><input type='checkbox' checked='false'></td>";一样写(不需要级联)

其次,查看此链接:http://php.net/manual/en/function.mysql-fetch-assoc.php

这个扩展在PHP 5.5.0中被弃用,在PHP 7.0.0中被删除。

第三,为了"更改对应复选框的记录值"您基本上需要将数据发送到服务器上的某个脚本(form->changeDbEntry.php),该脚本应该连接到DB并完成您想要的工作。

我知道这不是你所期望的答案,但不了解发生了什么的解决方案几乎是无用的。所以…

建议1:阅读html表单、POST/GET请求和AJAX(稍后阅读标题、WebSockets、协议…)

建议2:看看这个:http://www.php-fig.org/psr/psr-1/