Checkbox inside a php foreach loop to delete whatever is checked

微笑、不失礼 提交于 2019-12-11 09:03:09

问题


    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?


回答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.




回答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
}
?>



回答3:


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



来源:https://stackoverflow.com/questions/21410765/checkbox-inside-a-php-foreach-loop-to-delete-whatever-is-checked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!