1

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?

1
  • Its much easier to use java script to submit your form. This gives you more control on which data to submit and manipulate it before sending as well. Commented May 23, 2016 at 10:10

2 Answers 2

1

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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

ohhh still not working!! let me know where you facing error? I will surely help you. @artur47wien
have you added your edited code here in question? If no then please add it so i can check and debug it from there. @artur47wien
First of all have you checked data is coming or not? please check by print_r($_POST) and give me that array output. @artur47wien
I got your issue. You need to create common delete button only one and that submit the form and redirect to product.php page so that you will get post data on that page. Right now its null because you have not used form to pass data. @artur47wien
from your code you can delete only one record on delete clicked.
|
1

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.

2 Comments

I was trying to do that with that 2nd way you said, the button which wasn't in the form was deleting the records, but still only when I wrote the id in the form
@artur47wien for the 2nd method u need to have a form written in your view file. Or probably append in run time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.