Erase data by checkbox

0

Well I'm here with a problem I have in my system categories in which I enter the products in the various categories that can choose through checkbox only I am now facing a problem and I want to delete a record eg: p>

I have a checkbox selected and it is stored in the database, so when I uncheck this box it goes out in the database. I tried to build the code here, but now you are not inserting or deleting 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() );
                                }
                            }
                        }
                    //}
                    ?>
    
asked by anonymous 16.02.2015 / 19:34

1 answer

1

Considering the options that the user proposed 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 errors please let me know.

    
16.02.2015 / 19:55