mardi 30 mars 2021

updating db with multiple checkbox value witch ID hidden sometimes fails

Before I had text type input, I simply put 1 for active and 0 to deactivate, everything works perfect like this, but I want to change the appearance of the app by checkbox.

When deactivating all and sending it works fine, also when turning off everything inserts well, but for example, I have 3 IDs (1,2,3), if I turn off the ID2 it actually turns off the ID3, I don't understand what happens.

This is what my table looks like

id - option - value

1 - option1 - 1
2 - option2 - 0
3 - option3 - 1

Code:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $item_Count = count($_POST["id"]);

  for($i = 0; $i < $item_Count; $i++) {

    DB::update('app_options', array(
      'active' => $_POST["active"][$i],
    ), "id = %s", $_POST["id"][$i]);

  }
}
?>

<form method="POST">
  <?php

  $results = DB::query("SELECT * FROM app_options");

  foreach ($results as $row) { ?>

    <input type="hidden" name="id[]" value="<?php print($row['id']);?>">

    <input class="form-check-input" type="checkbox" name="active[]" id="active" value="1" <?php if (isset($row['active'])){echo "checked";} ?> >

  <?php } ?>

  <button type="submit" class="btn btn-primary btn-block">submit</button>
</form>

There are many questions with answers, but none use the hidden input that I need, I hope you can help me, thank you very much!




Aucun commentaire:

Enregistrer un commentaire