This question is very similar to Delete multiple rows with PDO and checkboxes using prepared statements?, but none of the answers suggested there worked in my case.
I have the product list page, where all the products from a database are listed using PHP
class method. Each product has a checkbox, and in <HTML>
part, this whole thing is wrapped in a <form>
which submits on the click of a button. Then, in the end of the main page I call another PHP
method from another class to delete the check-boxed products, based on one of the answers given in the similar question I mentioned above:
class MassDelete
{
public function deleteSelected()
{
if ($_SERVER['REQUEST_METHOD'] == "POST") {
print_r($_POST);
}
$skus = array();
foreach ($_POST['delete'] as $pval) {
$skus[] = strval($pval);
}
$skus = implode(',', $skus);
$pdo = new Database;
$conn = $pdo->getConnection();
$query = $conn->prepare("DELETE FROM `products` WHERE `sku` IN ( $skus )");
$query->execute();
}
}
The problem is - it doesn't work. By using print_r($_POST);
I see that $_POST
gets the values I need (SKUs) from the respective products, so the problem shouldn't lie within the <HTML>
part or the class that is responsible for displaying the products on this page.
Therefore, I believe it is this delete class and PDO statement to blame. Any suggestions on what to do/where to look?