[英]How to delete records from database using checkbox and button which are not in the form
I have code like this in product.php: EDIT: 我在product.php中有这样的代码:编辑:
class Product {
private $conn;
private $id;
private $name;
private $description;
private $price;
private $category_id;
private $category_name;
private $created;
public function __construct($db) {
$this->conn = $db;
}
public function readAll()
{
$stmt = $this->conn->prepare('SELECT id, name, description, price, CategoryID, created FROM products');
$stmt->execute();
echo "<table class=\"highlight responsive-table\">
<thead>
<tr>
<th data-field=\"empty\"> </th>
<th data-field=\"name\">Name</th>
<th data-field=\"description\">Description</th>
<th data-field=\"price\">Price</th>
<th data-field=\"category\">Category</th>
<th data-field=\"action\">Action</th>
</tr>
</thead>";
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
$id = $result['id'];
$n = $result['name'];
$d = $result['description'];
$p = $result['price'];
$ca = $result['CategoryID'];
$c = $result['created'];
echo "<tbody>
<tr>
<td style=\"width:10%;\">
<input type=\"checkbox\" id=\"checkbox_".$id."\" name=\"checkbox[]\" />
<label for=\"checkbox_".$id."\"></label>
</td>
<td style=\"width:15%;\">" .$n. "</td>
<td style=\"width:30%;\">" . $d. "</td>
<td style=\"width:10%;\">" ."$".$p. "</td>
<td style=\"width:15%;\">" . $ca. "</td>
<td style=\"width:20%;\">
<a class=\"waves-effect waves-light btn\"><i class=\"material-icons\">mode_edit</i></a>
<a class=\"waves-effect waves-light btn\"><i class=\"material-icons\">delete</i></a>
</td>";
}
echo "</tbody> </table>";
}
public function deleteSelected($ids) {
$query = 'DELETE FROM products WHERE id=?';
$stmt = $this->conn->prepare($query);
if (is_array($ids)) {
foreach ($ids as $id)
$stmt->execute([$id]);
}
else {
$stmt->execute([$ids]);
}
}
}
/* ****** */
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if (isset($_POST['delete']) && !empty($_POST['checkbox'])) {
$checkboxArr = $_POST['checkbox'];
foreach ($checkboxArr as $row) {
$checkIds = explode("_", $row);
$id = $checkIds[1];
$cat = new Product($conn);
//$id = filter_input ( INPUT_POST , 'id', FILTER_SANITIZE_NUMBER_INT );
$cat->deleteSelected($id);
}
}
}
and the button: 和按钮:
<button class="btn-floating btn-large waves-effect waves-light red" type="submit" value="Delete" name="delete">
<a><i class="material-icons">clear</i></a>
</button>
I'm displaying content of database in a table in index.php using readAll function. 我正在使用readAll函数在index.php中的表中显示数据库的内容。 In 1st column I have checkbox.
在第一栏中,我有一个复选框。 On the page are also few buttons, one of them is supposed to delete selected records from the database.
页面上还有几个按钮,其中一个按钮应该从数据库中删除选定的记录。 I can do that using form like this:
我可以使用这样的形式来做到这一点:
<form action="" method="post">
<input type="text" name="id" id="id" placeholder="Please Enter Id"/><br /><br />
<input type="submit" value="Delete" name="delete" id="delete"/><br />
but how can I delete the record, without writing it's id in any form, just by using checkbox (in product.php) and button (in index.php) which is not in the form? 但是如何通过不使用该表单中的复选框(在product.php中)和按钮(在index.php中),而无需以任何形式写入其ID的情况下删除记录呢?
Using ajax , you can send a set of ID's to delete to a server code. 使用ajax,您可以发送一组ID来删除到服务器代码。 Bind to the delete buttons onClick event using javascript ( ie. Pure, jQuery)
使用javascript(即Pure,jQuery)绑定到删除按钮onClick事件
Or you can also trigger a form submission and set the id's to a hidden input. 或者,您也可以触发表单提交并将ID设置为隐藏的输入。 And do it in a non-ajax fashion.
并以非ajax的方式进行。
Use name="checkbox[]"
and id = "checkbox_".$id."
. 使用
name="checkbox[]"
和id = "checkbox_".$id."
。
Using checkbox[]
you will get multiple selected checkbox with its id in comma separated. 使用
checkbox[]
您将获得多个选中的复选框,其ID以逗号分隔。
class Product {
private $conn;
private $id;
private $name;
private $description;
private $price;
private $category_id;
private $category_name;
private $created;
public function __construct($db) {
$this->conn = $db;
}
public function readAll()
{
$stmt = $this->conn->prepare('SELECT id, name, description, price, CategoryID, created FROM products');
$stmt->execute();
echo " <form action="./objects/product.php" method="post">
<table class=\"highlight responsive-table\">
<thead>
<tr>
<th data-field=\"empty\"> </th>
<th data-field=\"name\">Name</th>
<th data-field=\"description\">Description</th>
<th data-field=\"price\">Price</th>
<th data-field=\"category\">Category</th>
<th data-field=\"action\">Action</th>
</tr>
</thead>";
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
$id = $result['id'];
$n = $result['name'];
$d = $result['description'];
$p = $result['price'];
$ca = $result['CategoryID'];
$c = $result['created'];
echo "<tbody>
<tr>
<td style=\"width:10%;\">
<input type=\"checkbox\" id=\"checkbox_".$id."\" name=\"checkbox[]\" />
<label for=\"checkbox".$id."\"></label>
</td>
<td style=\"width:15%;\">" .$n. "</td>
<td style=\"width:30%;\">" . $d. "</td>
<td style=\"width:10%;\">" ."$".$p. "</td>
<td style=\"width:15%;\">" . $ca. "</td>
<td style=\"width:20%;\">
<a class=\"waves-effect waves-light btn\"><i class=\"material-icons\">mode_edit</i></a>
<a class=\"waves-effect waves-light btn\"><i class=\"material-icons\">delete</i></a>
</td>";
}
echo " <input type="submit" value="Delete" name="delete" id="delete"/></form>";
echo "</tbody> </table>";
}
public function deleteSelected($ids) {
$query = 'DELETE FROM products WHERE id=?';
$stmt = $this->conn->prepare($query);
if (is_array($ids)) {
foreach ($ids as $id)
$stmt->execute([$id]);
}
else {
$stmt->execute([$ids]);
}
}
/* ****** */ / * ****** * /
In below code we are getting comma separated selected checkbox array and delete it using foreach loop of $_POST['checkbox']
array. 在下面的代码中,我们用逗号分隔了选中的复选框数组,并使用
$_POST['checkbox']
数组的foreach循环将其删除。
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if ( isset( $_POST['delete']) && !empty( $_POST['checkbox']) ) {
$checkboxArr = $_POST['checkbox'];
foreach($checkboxArr as $id)
{
$cat = new Product($conn);
//$id = filter_input ( INPUT_POST , 'id', FILTER_SANITIZE_NUMBER_INT );
$cat->deleteSelected($id);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.