0

我正在使用复选框按钮来更新日历中每月一天的占用情况。

如果我只选中一个复选框,它会更新它,但如果我选中多个复选框,它不会更新任何行。

这是我的代码:

$query = mysqli_query($con, "SELECT * FROM koledar");
while ($vsidnevi = mysqli_fetch_assoc($query)) {
    $dnevi = [$vsidnevi['dan']];
    foreach ($dnevi as $dan) {
        if ($vsidnevi['zasedenost']==0) {
            echo '<label class="btn btn-primary">';
            echo '<input type="checkbox"  name="miha" value="' . $dan . '" data-toggle="button">' . $dan;
            echo '</label>';
        } elseif ($vsidnevi['zasedenost']==1) {
            echo '<label class="btn btn-primary active disabled">';
            echo '<input type="checkbox" name="miha" value="' . $dan . '" data-toggle="button">' . $dan;
            echo '</label>';
        }
    }
}

if (isset($_GET['dodaj']) && $_GET['dodaj']=="true") {
    if(isset($_POST['miha'])) { 
        $daen = $_POST['miha'];
        $dodaj = mysqli_query($con, "UPDATE koledar SET zasedenost=1 WHERE dan=" . $daen . "");
    } 
}
4

1 回答 1

0

首先,您应该将您的文字值作为参数传递给 MySQL(以抵御 SQL 注入攻击)。

当提交多个值时,$_POST['miha']将是一个您必须循环的数组:

if (isset($_GET['dodaj']) && $_GET['dodaj']=="true") {
    if(isset($_POST['miha'])) { 
        $dodaj = mysqli_prepare($con, '
            UPDATE koledar
            SET    zasedenost=1
            WHERE  dan=?
        ');
        mysqli_stmt_bind_param($dodaj, 's', $daen);
        foreach ($_POST['miha'] as $daen) {
            mysqli_stmt_execute($dodaj);
        }
    } 
}

否则使用IN ()

if (isset($_GET['dodaj']) && $_GET['dodaj']=="true") {
    if(isset($_POST['miha'])) {
        $inq = implode(',', array_fill(0, count($_POST['miha']), '?'));
        $dodaj = mysqli_prepare($con, "
            UPDATE koledar
            SET    zasedenost=1
            WHERE  dan IN ($inq)
        ");

        call_user_func_array('mysqli_stmt_bind_param', array_merge(
            array(
                $dodaj,
                str_repeat('s', count($_POST['miha']))
            ),
            $_POST['miha']
        ));
        mysqli_stmt_execute($dodaj);
    } 
}
于 2013-10-28T18:31:52.437 回答