I have code like this in product.php: EDIT:
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. 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?
Using ajax , you can send a set of ID's to delete to a server code. Bind to the delete buttons onClick event using javascript ( ie. Pure, jQuery)
Or you can also trigger a form submission and set the id's to a hidden input. And do it in a non-ajax fashion.
Use name="checkbox[]"
and id = "checkbox_".$id."
.
Using checkbox[]
you will get multiple selected checkbox with its id in comma separated.
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.
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);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.