1

enter image description here

    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>";



?>

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?

0

3 Answers 3

2

Name all the checkboxes a same, like delete[] , and and put the name of product in the value of each checkbox.

Example :

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

1 Comment

it gives me this error: Notice: Undefined index: delete in /home/michel/public_html/admin_selecteren_voor_verwijderen.php on line 63
1

You're looking for $_POST['checkbox'] but that's not what you have in your form. Name the checkboxes all checkbox[] and use $product_naam as the value.

<input type='checkbox' name='checkbox[]' value='$product_naam'>

Now you can loop over it and delete with your foreach loop.

3 Comments

Thank! your method works.. but how do I know delete all the selected products?
How do you know? I don't understand. You could echo after each delete, or log it, or just go back to the other page and refresh.
I found it out. Thanks a lot for your help!
0

you should not name the checkbox to the product name, just call it delete or something. Then you can use the foreach method to delete them from the database, like this:

<?php

    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=delete' 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(isset($_POST['delete'])) {
    foreach($_POST['delete'] as $delete){ {
        $sql = "DELETE FROM producten WHERE product_naam= $delete"; 
        $query = $dbh->prepare( $sql );
        $result = $query->execute();

    }
}





    echo "  
    <input type='submit' value='Delete'>
    </form>";



?>

Also, it seems to me you're mistaking "value" with "name", read up on it. And it would be good to read something about PDO, mysql isn't safe. http://www.php.net/manual/en/book.pdo.php

1 Comment

i get this error : Warning: Invalid argument supplied for foreach() in /home/michel/public_html/admin_selecteren_voor_verwijderen.php on line 63

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.