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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
michAmir
  • 375
  • 2
  • 4
  • 12

3 Answers3

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
}
?>
Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57
  • it gives me this error: Notice: Undefined index: delete in /home/michel/public_html/admin_selecteren_voor_verwijderen.php on line 63 – michAmir Jan 28 '14 at 16:59
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.

Digital Chris
  • 6,177
  • 1
  • 20
  • 29
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

vincent kleine
  • 724
  • 1
  • 6
  • 22
  • i get this error : Warning: Invalid argument supplied for foreach() in /home/michel/public_html/admin_selecteren_voor_verwijderen.php on line 63 – michAmir Jan 28 '14 at 16:51