【问题标题】:Uncheck checkbox value always at the bottom of the list in mysql取消选中 mysql 列表底部的复选框值
【发布时间】:2013-08-01 17:40:01
【问题描述】:

我有一张带有复选框的桌子。我使用数组来处理这个。但不知何故提交后。取消选中的值始终位于 mysql 列表的底部。这是我的代码:

$mystudentID = $_POST['studID']; 
$mystudATT = $_POST['myattt']; 
$number = count($mystudentID ); 

for ($i=0; $i<$number; $i++) 
{
    $studID = $mystudentID[$i]; 
    $studAtt = $mystudATT[$i]; 

    $sql="
        INSERT INTO mate_student_att
        (attendance,date,studID) 
        VALUES 
        ('$studAtt','$_POST[myDate]','$studID')
    ";

    if (!mysqli_query($con,$sql))
    {
        die('Error: ' . mysqli_error($con));
    }
    echo "1 record added";
} 
mysqli_close($con);

请帮忙

【问题讨论】:

  • 问题不清楚先生
  • 我的意思是我的未选中框从未进入我的阵列。我们如何将未选中的值存储到 mysql db
  • 未选中的复选框不会通过表单发布传输,没有给定的复选框是确定它未被选中
  • 是的,我刚刚了解到这一点。有什么地方可以解决这个问题吗?
  • 我之前的评论也写过。无法更改表单发布行为,您必须通过确定复选框何时不存在、未选中来调整代码。

标签: php mysql arrays checkbox


【解决方案1】:

DevZer0 是对的。 将所有复选框保留在数据库中的唯一方法是在 html 表单中创建一个隐藏字段,并显示所有复选框(无论它们的初始状态是什么)。 例如,您可以像这样保留所有复选框名称

 studID1,studID2,studID3,studID4 

在隐藏字段的值中。 此隐藏字段将被张贴。 然后,您必须拆分/分解隐藏字段的值才能获得带有复选框名称的数组。 您循环进入该数组,如果在您的帖子参数中找到每个复选框名称的名称,则表示他的复选框已被选中,您将其插入到您的数据库中,出勤率为 1,否则您将其插入您的数据库,出勤率为 0。 如果您无法完成,我可以相应地更改您的代码。

编辑

你的表格

<input type='hidden' name='allStudIDs' value='studID1,studID2,studID3,studID4,studID5,studID6,studID7' />
<input type='checkbox' name='studID1" />student 1
<input type='checkbox' name='studID2" />student 2
<input type='checkbox' name='studID3" />student 3
<input type='checkbox' name='studID4" />student 4
<input type='checkbox' name='studID5" />student 5
<input type='checkbox' name='studID6" />student 6
<input type='checkbox' name='studID7" />student 7

和你发布后的脚本

$allStudentIDs = $_POST['allStudIDs']; 
$mystudentID = $_POST['studID']; 
$tempArray = explode(",", $allStudentIDs);

for ($i=0; $i<count($tempArray); $i++) 
{
    $studID = $tempArray[$i];
    if (isset($_POST[$tempArray[$i]])) 
    {
        $studAtt = 1; 
    }
    else
    {
        $studAtt = 0; 
    }

    $sql="
        INSERT INTO mate_student_att
        (attendance,date,studID) 
        VALUES 
        ('$studAtt','$_POST[myDate]','$studID')
    ";

    if (!mysqli_query($con,$sql))
    {
        die('Error: ' . mysqli_error($con));
    }
    echo "1 record added";
} 
mysqli_close($con);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多