Estoy intentando borrar multiples filas a travez de un checkbox, busque mucho e incluso utilice chatGPT pero no consigo solucionarlo, me borra una sola fila pero cuando selecciono varias no lo hace. Para acceder a ellas estuve utilizando setters y getters.
Este es el codigo:
productClass.php
//Method to delete in database
public function deleteProduct()
{
try{
$stm= $this->conn->prepare("DELETE FROM product WHERE sku IN (?)");
$stm->execute([$this->sku]);
return $stm->fetchAll();
}
catch(Exception $e)
{
return $e->getMessage();
}
}
Index.php (donde se visualiza y esta el checkbox)
<form action="product/delete.php" method="post" id="product_list">
<?php
require_once("product/productClass.php");
$take = new Product();
$product = $take->showAll();
foreach ($product as $key => $prod):?>
<div class="col-sm-2 border border-dark my-5 container-fluid shadow">
<input type="checkbox" name="deleteSku[]" id="delete-checkbox" value="<?= $prod['sku']?>">
<h5 class="text-center"><?= $prod['sku']?></h5>
<p class=" text-center"><?= $prod['name']?></p>
<p class=" text-center"><?= $prod['price']?>$</p>
<p class=" text-center">
<?php echo $prod['size'] != 0 ? "Size: " . $prod['size'] . " " ."MB" : '';
echo $prod['weight'] != 0 ? "Weight: " . $prod['weight'] . " " . "KG" : '';
echo $prod['height'] && $prod['width'] && $prod['length'] != 0 ? "Dimensions: " . $prod['height']."x".$prod['width']."x".$prod['length'] : "" ; ?> </p>
<button class="btn btn-danger" type="submit" name="delete" id="delete-product-btn">MASS DELETE</button>
</div>
<?php endforeach ?>
</form>
delete.php
<?php
require_once("productClass.php");
$take = new Product();
if(isset($_POST['delete']))
{
if(isset($_POST['deleteSku']) && is_array($_POST['deleteSku']))
{
foreach ($_POST['deleteSku'] as $record);
$take->setSku($record);
$take->deleteProduct();
header("location:../index.php");
}
}
?>
Espero que se entienda que es lo que quiero hacer, cualquier ayudita me sirve. Gracias.