Xibition Xibition - 4 months ago 11
PHP Question

Insert query in DB from checkbox count

I have some checkboxes in my HTML page. When a user chose for example six checkboxes, I want to save it into a database like 1 for the first checkbox, 2 for the second .... and 6 for the last checked:

foreach ($tape as $tap) {
//some variables
// I have tried this but it gives me 1 everytime.
$i = 1;
$i = sprintf('%02d', $i);
$array = array($the_date, $i);
$ref = implode(".Tape.", $array); // for 2016_73_98.Tape.01

$stmt = $pdo->prepare(
"INSERT INTO test (server, tape, 'date', reference, tape_name)
VALUES (:server, :tape, :the_date, :reference, :tape_name)"
);
$stmt->bindParam(':server', $server);
$stmt->bindParam(':tape', $tap);
$stmt->bindParam(':the_date', $the_date);
$stmt->bindParam(':reference', $my_tape);
$stmt->bindParam(':tape_name', $ref);

$result = $stmt->execute();
$i++;
}


I tried a for loop with the
$count
which counts how many checked elements, but it inserts 6 times the same choice for each of the 6 inputs.
image

I want to replace in the "tape_name" column, 01 with (01 for the first MA0001, 02 for MA0002 .... ) It doesn’t have anything to do with the reference, it has to be following the number of checked checkboxes.

<?php

for ($i = 1; $i < 101; $i++): //adjust this number to whatever number of checkboxes you want
echo '<div class="each_checkboxes">';
echo '<label for="checkbox' . $i . '">' . $i . '</label>';
echo '<input type="checkbox" name="tape[]" id="checkbox' . $i . '" value="' . $i . '"/>';

echo '</div>';

endfor;
?>

Answer

You don't even need to use $i incrementation here, because you are already storing the incremented number in the checkboxes.

You need to just fetch the selected checkbox value like this.

foreach ($tape as $tap) {
    //some variables
    // I have tried this but it gives me 1 everytime. 
    $i = sprintf('%02d', $tap);
    $array = array($the_date, $i);
    $ref = implode(".Tape.", $array); // for 2016_73_98.Tape.01

    $stmt = $pdo->prepare(
            "INSERT INTO test (server, tape, 'date', reference, tape_name) 
             VALUES (:server, :tape, :the_date, :reference, :tape_name)");

    $stmt->bindParam(':server', $server);
    $stmt->bindParam(':tape', $tap);
    $stmt->bindParam(':the_date', $the_date);
    $stmt->bindParam(':reference', $my_tape);
    $stmt->bindParam(':tape_name', $ref);

    $result = $stmt->execute();
}

Edited

Now if you have selected 5th checkbox and 10th checkbox only, then the 5th selected checkbox will get tape 01 and 10th selected checkbox will get tape 02. In that scenario following code will useful

$i = 1;
foreach ($tape as $tap) {
    //some variables
    // I have tried this but it gives me 1 everytime. 
    $i = sprintf('%02d', $i);
    $array = array($the_date, $i);
    $ref = implode(".Tape.", $array); // for 2016_73_98.Tape.01

    $stmt = $pdo->prepare(
            "INSERT INTO test (server, tape, 'date', reference, tape_name) 
             VALUES (:server, :tape, :the_date, :reference, :tape_name)");

    $stmt->bindParam(':server', $server);
    $stmt->bindParam(':tape', $tap);
    $stmt->bindParam(':the_date', $the_date);
    $stmt->bindParam(':reference', $my_tape);
    $stmt->bindParam(':tape_name', $ref);

    $result = $stmt->execute();
    $i++;
}

Let me know if it works for you.


Edited after understanding further requirement

Add following jquery code in your script

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function () {

    $("input[type='checkbox']").click(function(){

       var cnt = Number($("#store_count").val());

        var checked_index = $(this).val(); alert("checked_index:" + checked_index);

        if($(this).is(":checked")){

            $(this).val(cnt); $("#store_count").val(cnt + 1);
        }
        else{

            $(this).val("");

            $("input[type='checkbox']:checked").each(function(){

                if( $(this).val() > checked_index ){ $(this).val(Number($(this).val()) - 1); }
            });

            $("#store_count").val(cnt - 1);
        }
    });
});
</script>

then make following changes in your HTML code, we need one hidden field to keep track of the counter.

<?php
for ($i = 1; $i < 10; $i++): //adjust this number to whatever number of checkboxes you want

    echo '<div class="each_checkboxes">';
    echo '<label for="checkbox' . $i . '">' . $i . '</label>';
    echo '<input type="checkbox" name="tape[]" id="checkbox' . $i . '" value=""/>';
    echo '</div>';

endfor;

echo '<input type="hidden" id="store_count" value="1" />';
?>

And finally your inserting data into database code

You don't even need to use $i incrementation here, because you are already storing the incremented number in the checkboxes.

You need to just fetch the selected checkbox value like this.

foreach ($tape as $tap) {
    //some variables
    // I have tried this but it gives me 1 everytime. 
    $i = sprintf('%02d', $tap);
    $array = array($the_date, $i);
    $ref = implode(".Tape.", $array); // for 2016_73_98.Tape.01

    $stmt = $pdo->prepare(
            "INSERT INTO test (server, tape, 'date', reference, tape_name) 
             VALUES (:server, :tape, :the_date, :reference, :tape_name)");

    $stmt->bindParam(':server', $server);
    $stmt->bindParam(':tape', $tap);
    $stmt->bindParam(':the_date', $the_date);
    $stmt->bindParam(':reference', $my_tape);
    $stmt->bindParam(':tape_name', $ref);

    $result = $stmt->execute();
}
Comments