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;
– Rafael Withoeft
'cause the whole deleting thing would have worked but I wanted to delete only the ones that aren’t selected
– César Sousa
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...
– Rafael Withoeft