0

I'm using checkbox buttons for updating occupation of day in month in my calendar.

If I check just one checkbox it updates it, but if I check multiple checkboxes it doesn't update any row.

Here is my code:

$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>';
        }
    }
}

and

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 . "");
    } 
}
1

1 Answer 1

0

Firstly, you should be passing your literal values to MySQL as parameters to a prepared statement (in order to defeat SQL injection attacks).

When multiple values are submitted, $_POST['miha'] will be an array over which you must loop:

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);
        }
    } 
}

Or else use 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);
    } 
}
Sign up to request clarification or add additional context in comments.

1 Comment

@user2925624: Would you care to be more explicit?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.