Save checkbox fields that are displayed through a WHILE with PHP?

Asked

Viewed 225 times

-1

I have a table called tarefas_check, when there is record in this table, the record is displayed this way:

<!-- Checklist -->
<form method="post" action="">
<?php

$sql = "SELECT * FROM tarefas_check WHERE ID_tarefas = '$ID_tarefas'";
$resultado = mysqli_query($con, $sql);
while ($cont = mysqli_fetch_array($resultado)) { ?>
 
// Os registros são exibidos como checkboxs...
<input type="checkbox" name="status[]" <?php if($cont['status']=="1"){ echo "checked";}?>/>
<label><?php echo $cont['texto']; ?></label><br>

<?php } // End while ?>

<!-- Button salvar checklist -->
<button type="submit" name="salvar_check" class="btn-success">Salvar checklists</button>
</form> 

The idea is this: When I select a checkbox and save, I want to give a UPDATE in the status field of that same table, but only in checkbox selected...

The problem is that when I select some and save, the UPDATE is being on every record, why is this happening?

UPDATE CODE:

<?php 
// Multiplo insert de checkbox
if(isset($_POST['salvar_check'])){ 
   $checkbox = $_POST['status'];
   $result = count($checkbox);

for ($i = 0; $i < $result; $i++) {  
  $query="UPDATE tarefas_check SET status = 1 WHERE ID_tarefas = '$ID_tarefas'";  
  mysqli_query($con, $query) or die("Não foi possível armazenar no banco");
};

I appreciate any kind of help.

1 answer

1


Hello,

Try modifying a little to send the ID of each task:

  1. When generating the checkboxes
    ...
    while ($cont = mysqli_fetch_array($resultado)) { ?>
    // Os registros são exibidos como checkboxs...
    // Repare abaixo que a variavel $idTarefa deve vir do banco de dados, igual 'status' e o 'texto'
    <input type="checkbox" name="status[<?=$idTarefa?>]"
    <?php if($cont['status']=="1"){ echo "checked";}?>/>
    <label><?php echo $cont['texto']; ?></label><br>
    <?php } // End while ?>
    ...
  1. at the time of processing:
    ...
    //aqui abaixo, passe por todos os resultados do POST e em cada iteração
    // inicialize a variável $id com o id que veio do HTML no name do checkbox
    foreach ($checkbox as $id => $c){
        $query="UPDATE tarefas_check SET status = 1 WHERE ID_tarefas = '$id'";
        mysqli_query($con, $query) or die("Não foi possível armazenar no banco");
    };
    ...
  • Thanks for the answer, it worked! + 1 . How would I do if I wanted to cancel? And back UPDATE from the status to 0?

  • In that case, you would have to create a different strategy. Since the HTML form only sends checkboxes that are marked, you would have to go through them all and uncheck those that are not present. You can do a for loop, and put all the Ids that came in a string, separated by comma, and use an sql that looks like this: UPDATE tarefas_check SET status = 0 WHERE Id_tasks NOT IN (1,2,3,4) In this case, tasks 1, 2, 3 and 4 will go back to zero and the rest will be as they are (either 0, or 1)

  • Send me an email that I explain better: manalista at gmail dot com

  • Hello mark, I believe you typed your email wrong. I tried to put the selected ID in an array and separate with comma as you said, I did so: $checks_selecionados = array($id);&#xA; echo implode(',',$checks_selecionados);, the selected Ids are displayed, but not separated by comma... (I placed these 2 lines within the foreach)

  • 1

    Hello... The email is right, just trade at for @and dot for . | About putting the ids in the array, create it outside the for like this: $selected = array(), and use it inside the for like this: $selected[] = $id. Note that at startup you call the array() function and in the assignment you do not inform the placement, with the brackets [], it means that it is within the same array, in the next position.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.