-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.
Thanks for the answer, it worked! + 1 . How would I do if I wanted to cancel? And back
UPDATE
from the status to 0?– Samuel Verissimo
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)
– Marcos Alexandre
Send me an email that I explain better: manalista at gmail dot com
– Marcos Alexandre
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);
 echo implode(',',$checks_selecionados);
, the selected Ids are displayed, but not separated by comma... (I placed these 2 lines within theforeach
)– Samuel Verissimo
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.
– Marcos Alexandre