当我选中多个选中的复选框时,复选框不会保留所有值

时间:2016-11-17 16:46:59

标签: php mysqli

我想插入员工出勤。当我检查了多个复选框时,它取两个复选框值1和其他复选框值0。所以如何解决这个问题请帮助我。这是我的表格代码

<form action="coll.php" method="post" name="create_grading" id="create_grading">  
        <table width="30%" border="0" cellpadding="2" cellspacing="3" class="mainTable">
            <tr>
                <th><input type="checkbox" id="selectall" /></th>
                <th>name</th>
            </tr>
            <?php
            $sql = "select * from employee";
            $query = mysqli_query($con, $sql);
            while ($row = mysqli_fetch_array($query)) {
                ?>   
                <tr>
                    <td><input type="hidden" name="eid[]" value="<?php echo $row['eid']; ?>"/>
                        <input name="status[]" class="case" type="checkbox" value="1" /><input name="status[]" class="case" type="hidden" value="0" /></td>
                    <td align="center"><?php echo $row['employee_name'] ?></td>
                </tr>
            <?php }; ?>

            <tr>
                <td></td>
                <td><input type="submit" name="Submit" id="Submit" value="Submit" /></td>       
            </tr>
        </table>
    </form>

这是我的插入代码

$host = "localhost";
$user = "root";
$pass = "";
$db = "multiple_row_insert";
$con = mysqli_connect($host, $user, $pass, $db);


if (isset($_POST['Submit'])) {
    $eid = $_POST['eid'];
    $count = count($eid);
    for ($i = 0; $i < $count; $i++) {
        $status = $_POST['status'][$i];
        $eid2 = $_POST['eid'][$i];
        $query = "INSERT INTO time(eid,status) VALUES ('$eid2','$status')";
        $query = mysqli_query($con, $query);
    }
}

1 个答案:

答案 0 :(得分:0)

if(isset($_POST['checkbox']))
//then is checked...
else
//is not checked

所以基本上你只需要1个复选框,你可以更改输入的名称。然后在你的后端使用类似的东西:

$_POST['status_'.$i]

在每个案例中检测是否使用“isset”检查了此$ _POST。

<强>编辑:

您的代码看起来应该是这样的(未经过测试,对不起,如果有任何语法错误,您可以修改它们):

$host = "localhost";
$user = "root";
$pass = "";
$db = "multiple_row_insert";
$con = mysqli_connect($host, $user, $pass, $db);


if (isset($_POST['Submit'])) {
    $eid = $_POST['eid'];
    $count = count($eid);
    for ($i = 0; $i < $count; $i++) {
        $status = 0;
        if(isset($_POST['status_'.$i])) //check if checkbox is setted (value 1 / checked)
          $status = 1;
        $eid2 = $_POST['eid'][$i];
        $query = "INSERT INTO time(eid,status) VALUES ('$eid2','$status')";
        $query = mysqli_query($con, $query);
    }
}

你的前端代码:

<form action="coll.php" method="post" name="create_grading" id="create_grading">  
        <table width="30%" border="0" cellpadding="2" cellspacing="3" class="mainTable">
            <tr>
                <th><input type="checkbox" id="selectall" /></th>
                <th>name</th>
            </tr>
            <?php
            $sql = "select * from employee";
            $query = mysqli_query($con, $sql);
            $i = 0;
            while ($row = mysqli_fetch_array($query)) {
                ?>   
                <tr>
                    <td><input type="hidden" name="eid[]" value="<?php echo $row['eid']; ?>"/>
                        <input name="status_<?php echo $i; ?>" class="case" type="checkbox" value="0" /></td>
                    <td align="center"><?php echo $row['employee_name']; ?></td>
                </tr>
            <?php $i++; } ?>

            <tr>
                <td></td>
                <td><input type="submit" name="Submit" id="Submit" value="Submit" /></td>       
            </tr>
        </table>
    </form>