'PHP 7.4 form data returns empty string [closed]
I try to delete data from database
if (isset($_POST['checkbox']) && isset($_POST['delete_btn'])) {
$productId = $_POST['id'];
$stmt = $database->prepare("DELETE FROM product WHERE id = :id");
$stmt->execute(array(':id' => $productId));
}
here is my HTML
<form action="/" method="POST" class="list-btn-top">
<button class="btn" type="submit" name="delete_btn" id="delete-product-btn">Mass delete</button>
</form>
<?php foreach ($stmt as $product): ?>
<div class="container">
<form action="" method="POST">
<input class="delete-checkbox" name="checkbox" type="checkbox" value="<?= $product['product_id'] ?>">
</form>
<p class="text-line"><?= $product['product_id'] ?></p>
<p class="text-line"><?= $product['sku'] ?></p>
<p class="text-line"><?= $product['name'] ?></p>
<p class="text-line"><?= $product['price'] ?></p>
<p class="text-line"><?= $product['product_value'] ?></p>
</div>
<?php endforeach; ?>
Solution 1:[1]
The reason for not getting any values is because you close the form immediately after the submit button. So no other data belongs to that form. Either put the button in the same form as the checkbox or vice versa.
Solution 2:[2]
You should change the
$productId = $_POST['id'];
to
$productId = $_POST['checkbox'];
As you're sending the id in the checkbox value but in your code you are picking wrong index i.e id
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | schmauch |
| Solution 2 | BlackXero |
