复选框里面的php foreach循环删除任何被检查 [英] Checkbox inside a php foreach loop to delete whatever is checked
问题描述
echo "<form name='input' action='admin_selecteren_voor_verwijderen.php' method='post'>";
$sql_bestelling= "SELECT * FROM producten";
foreach($dbh->query($sql_bestelling) as $row)
{
$product_id=$row['product_id'];
$product_naam=$row['product_naam'];
$prijs=$row['prijs'];
$foto=$row['foto'];
echo "
<br>
<img src='$foto' height='70' width='50' border='0'>
<b>$product_naam</b> <input type='checkbox' name='$product_naam' value='$product_naam'></br>
</br></br></br>";
//if (isset($_POST['submit'])){
// $sql = "DELETE FROM `producten` WHERE product_naam='$product_naam'";
// $query = $dbh->prepare( $sql );
// $result = $query->execute();
//}
}
if(!empty($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $check) {
echo "check: ", $check;
}
}
echo "
<input type='submit' value='Delete'>
</form>";
?>
我想在我的网上商店管理员页面中有一个产品列表。每个产品都有一个复选框。我想能够删除所有复选框的产品都被选中。但我不知道怎么..以上是我到目前为止。添加的图片是它在页面上的样子。但是页面是从带有foreach循环的数据库中选择的产品的列表,如在代码中可以看到的。复选框也在循环中。我不知道如何分配每个产品的检查到一个变量,然后从数据库中删除它们。
I want to have a list of product in my webshop administrator page. Every product has a checkbox. I want to be able to delete all of the product of the checkboxes are checked. But I don't know how.. The above is what I have so far. The added picture is how it looks on the page. But the page is a list of product selected from the database with a foreach loop as can be seen in the code. The checkbox also is in the loop. I don't know how to assign every product which is check to a variable and then delete them from the database. Can anyone help me?
推荐答案
将所有复选框命名为相同,例如 delete []
,并将产品名称放在每个复选框的值中。
Name all the checkboxes a same, like delete[]
, and and put the name of product in the value of each checkbox.
<form action="..." method='post' >
user1<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
user2<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
user3<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
user4<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
<input type='submit' value='delete' />
</form>
删除查询:
Delete query :
<?php
if(isset($_POST['delete'])){
$ids = $_POST['delete'];
$sql = "DELETE FROM `producten` WHERE product_naam
IN('".implode("','", $ids)."')";
//execute query
}
?>
这篇关于复选框里面的php foreach循环删除任何被检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!