【问题标题】:How can I change a record in a database with a checkbox created with php如何使用用 php 创建的复选框更改数据库中的记录
【发布时间】:2016-07-21 14:04:23
【问题描述】:

我的复选框是使用 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>";
            }
        ?>

【问题讨论】:

  • 好吧,首先,您的复选框中没有一个具有name 属性,因此如果/当该表单被提交时,则无需提交任何内容。他们也没有value 属性,所以即使他们有名字,也没有数据可以连同名字一起提交,所以没有办法将任何特定的复选框与那些高重复性的Month_X 位中的任何一个相关联你正在测试。

标签: php sql checkbox


【解决方案1】:

我认为您应该为每个包含月份标志和学生 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']]
}

【讨论】:

  • 解析错误:语法错误,意外'' (T_ENCAPSED_AND_WHITESPACE),在第 28 行的 update_fees.php 中需要标识符 (T_STRING) 或变量 (T_VARIABLE) 或数字 (T_NUM_STRING),您有团队查看器吗?我猜这样会更容易......感谢您的帮助
  • 第 28 行的代码是什么?很遗憾现在无法使用团队查看器
  • $q_m1="UPDATE bio_fees SET Month_1=$Month_1 WHERE SID=$student['SID']";
  • 试试这个:$q_m1="UPDATE bio_fees SET Month_1={$Month_1} WHERE SID=" . $student['SID'];
【解决方案2】:

首先,这个echo "&lt;td&gt;"."&lt;input type='checkbox' checked='false'&gt;"."&lt;/td&gt;";不用写,像echo "&lt;td&gt;&lt;input type='checkbox' checked='false'&gt;&lt;/td&gt;";这样写就行了(那里不需要串联)

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

此扩展在 PHP 5.5.0 中被弃用,并在 PHP 7.0.0 中被删除。

三、为了“改变复选框对应的记录值” 您基本上需要将数据发送到服务器上的某个脚本( form -> changeDbEntry.php )并且该脚本应该连接到数据库并执行您想要的工作。

我知道这不是您期望的答案,但是不了解正在发生的事情的解决方案几乎是无用的。所以...

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

建议 2:检查一下:http://www.php-fig.org/psr/psr-1/

【讨论】:

    猜你喜欢
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多