Delete data by checkbox

Asked

Viewed 90 times

0

Well personal I am here with a problem I have in my system categories where I insert the products in the various categories you can choose through checkbox only now I am facing a problem and I want to delete a record for example:

I have a checkbox selected and this saved in the bank wanted that when unchecking this box it erases in the database I tried here to build the code only now I am not entering or erasing from the database.

Php code

<?php
                     if($_REQUEST['op']=="editar" && $_REQUEST['valida']=="ok"){
                        if($_REQUEST['op']=="editar"){
                            //$result_existe=mysql_query("select * from colecoes where activo=1");
                            //$row_existe=mysql_query($result_existe);
                            //$result_existe=mysql_query("select * from categorias_estabelecimentos where estabelecimento_id = '".$_REQUEST['id']."' and categoria_slug = '".$row_existe->slug."'");        
                            //if(mysql_num_rows($result_existe)>0){
                                if (!isset($_POST['categoria'])){
                                    foreach($_POST['categoria'] as $entry_categorias_delete){
                                    $categorias_delete= $entry_categorias_delete;
                                        $sql="DELETE FROM categorias_estabelecimentos WHERE estabelecimento_id = '".$_REQUEST['id']."' and categoria_slug='".$categorias_delete."'";
                                        mysql_query($sql) or die (mysql_error() );
                                    }
                                }
                            }else{
                                $checkBox = $_POST['categoria'];
                                $estabelecimento_id = $_REQUEST['id'];
                                foreach($checkBox as $entry_categorias){
                                $categorias= $entry_categorias;
                                    $query="INSERT INTO categorias_estabelecimentos (estabelecimento_id, categoria_slug) VALUES ('".$estabelecimento_id."', '".$categorias."')";     
                                    mysql_query($query) or die (mysql_error() );
                                }
                            }
                        }
                    //}
                    ?>
  • What happens is that in $_POST, you will only receive the ones that were "checked", so one of the solutions you can delete everything that is linked in that establishment (DELETE FROM categories_establishments;

  • 'cause the whole deleting thing would have worked but I wanted to delete only the ones that aren’t selected

  • Try using the NOT IN operator example: DELETE FROM emails WHERE id NOT IN (1,2,3,56); Replace what is needed for your case...

1 answer

1


Taking into account the options the user proposed I made the modifications where they were needed:

if (!isset($_POST['categoria'])){
    $categorias = '' . implode('\',\'', $_POST['categoria']);
    $id = $_REQUEST['id'];
    $sql= "DELETE FROM categorias_estabelecimentos WHERE estabelecimento_id = $id AND categoria_slug NOT IN ($categorias)";
    mysql_query($sql) or die (mysql_error() );
}

So it will delete everything that is not "selected" on the screen; I have not tested, any error please advise.

  • Deleting already works well but now I do not enter the Insert if you select some option

  • I already solved the question of the Internet was out of the if it already works thanks for the help

  • @Césarsousa Very good! Dispose

Browser other questions tagged

You are not signed in. Login or sign up in order to post.