Basically your form
may have this structure:
<form method="post">
<input type="checkbox" name="deletar[]" value="1" />Banana<br>
<input type="checkbox" name="deletar[]" value="2" />Pera<br>
<input type="checkbox" name="deletar[]" value="3" />Maçã<br>
</form>
Obviously, you will generate the inputs
in a loop in PHP, and in value will put the ID of each item to be deleted.
The "secret" here (which is no secret, you have in the PHP documentation) is to put the keys []
in the "name" property, for PHP to receive the data as array
and in PHP just that:
if(!empty($_POST['deletar'])) {
foreach($_POST['check_list'] as $id) {
// Aqui voce faz a operacao com o ID desejado
}
}
Riding the query delete:
To delete, effectively, you can use this syntax depending on the DB:
DELETE FROM minha_tabela WHERE ID IN ( id1, id2, id3, ... );
which can be easily mounted with PHP:
if(!empty($_POST['deletar'])) {
$query = 'DELETE FROM minha_tabela WHERE ID IN (';
$query .= implode( ',', $_POST['deletar'] );
$query .= ');';
// executa a query
}
The ideal is that this is optimized in order to group the results in batches, not to become gigantic the IN clause of the query:
if( !empty( $_POST['deletar'] ) ) {
$groups = array_chunk( $_POST['deletar'], 50 );
foreach ( $groups AS $group ) {
$query = 'DELETE FROM minha_tabela WHERE ID IN (' . implode( ',', $group ) . ');';
// executa a query
}
}
This way, every 50 records will be executed a query. For consistency, it may be the case to group everything into one transaction, but there already depends on the actual use case, as well as the sanitization of the input values.
Alternatives to other situations:
Nothing prevents you from using other structures, but it would be the case to choose the most suitable for the real case. Here is an "improvised" example of doing otherwise:
<form method="post">
<input type="checkbox" name="id_1" value="x" />Banana<br>
<input type="checkbox" name="id_2" value="x" />Pera<br>
<input type="checkbox" name="id_3" value="x" />Maçã<br>
</form>
and in PHP:
foreach( $listaDeIds as $id ) {
if( isset( 'id_' . $id ) {
// Aqui voce faz a operacao com o ID desejado
}
}
This second way is not suitable for your case because you need to have a list of Ids in advance, I just put as an example that there are several ways to move with the syntax depending on the context.
An example where this syntax would make sense is if instead of a checkbox to delete, you had one radiogroup by item, with options "delete", "archive", "do nothing", for example. But even so, there would be several other ways to solve the same problem.
Check this - http://answall.com/questions/92898/deletar-apenas-checkbox-selected-php/92917#92917
– Edilson
http://answall.com/a/7064/91
– rray
Post the entire error, not just a piece, missed to inform the file. A doubt, why your php is in French?
– Guilherme Nascimento
The error that appears on the page is only this. Well, whenever I get these type of errors with mysql database comes in French. In order to receive the error, I added an Else in the condition as follows: } Else ? echo mysqli_error($connects);
– Cobra