2

I have one table on my database that includes kategoriID,kategoriAd,kategoriEbeveyn. It is like categoryID,catagoryName and categoryParent.

I want that happens that is when i try to delete main category, I can delete main category and sub categories.

-World

............ +Europe

....................... *Turkey

.................................**Istanbul

When I try to delete, This function works for World and Europe. But Turkey and Istanbul is still in my database. I couldn't delete those because of the error.

"mysql_fetch_array() expects parameter 1 to be resource, boolean given in"

| kategoriID | kategoriAd | kategoriEbeveyn |

| 1 | World | 0 |

| 2 | Europe | 1 |

| 3 | Turkey | 2 |

| 4 | Istanbul | 3 |

Here is my function.

<?php

function KategoriVeAltKategorileriSil(){
$kategoriID = $_GET['kategoriID'];
     mysql_query("DELETE FROM kategoriler WHERE kategoriID = ' $kategoriID '") or die (mysql_error());
       $delete = mysql_query("DELETE FROM kategoriler WHERE kategoriEbeveyn = ' $kategoriID ' ") or die (mysql_error());        

    if($delete){
         while($silinecek = mysql_fetch_array($delete)){

               KategoriVeAltKategorileriSil($silenecek[' kategoriID ']);
             echo mysql_error();
                     }
    echo "Kategori ve alt kategorileri silindi.";
    }else{ echo "Silme işlemi gerçekleştirilemedi!" .mysql_error(); }
        }

?>

Or Do i need to use Trigger?

cell-in
  • 709
  • 2
  • 11
  • 27
  • Whats you table structure? – Prashank Mar 08 '13 at 11:28
  • 1
    You can't fetch for DELETE request because DELETE just return the number of deleted rows. If you wan't to fetch, use SELECT statement instead – JoDev Mar 08 '13 at 11:29
  • My guess is that this `while($silinecek = mysql_fetch_array($delete))`{ returns false. – Voitcus Mar 08 '13 at 11:29
  • You COULD set up database triggers instead of doing it in PHP. http://stackoverflow.com/questions/4798768/trigger-delete-from-2-tables-in-mysql – jtheman Mar 08 '13 at 11:31

3 Answers3

4
    function deleteCategory($kategoriID) {
        $result=mysql_query("SELECT * FROM kategoriler WHERE kategoriEbeveyn='$kategoriID'");
        if (mysql_num_rows($result)>0) {
             while($row=mysql_fetch_array($result)) {
                  deleteCategory($row['kategoriID']);
             }
        }
        mysql_query("DELETE FROM kategoriler WHERE kategoriID='$kategoriID'");
    }

you can use this recursive function
Nanhe Kumar
  • 15,498
  • 5
  • 79
  • 71
  • Thank you for your solution. But i still have an error on this line deleteCategory($row['kategoriID']); Undefined index: kategoriID in. Sorry i am newbie in php. Maybe something wrong with my switch case function. $Go = @$_GET['Go']; Switch($Go){ case"deleteCategory": deleteCategory(); break;} – cell-in Mar 08 '13 at 13:21
  • or i need to call this function like that in switch case function? case"deleteCategory": deleteCategory($kategoriID); break; – cell-in Mar 08 '13 at 13:51
  • if you are using above code then check kategoriID filed exist in your table yes you can use with case – Nanhe Kumar Mar 09 '13 at 07:35
0

The best way would be to make a request like :

DELETE FROM kategoriler WHERE ketegoriID IN (SELECT kategoriID
FROM kategoriler
WHERE kategoriID = ' $kategoriID ' or kategoriEbeveyn = ' $kategoriID ');

of course it's not complete because i don't know your databases structure

JoDev
  • 6,633
  • 1
  • 22
  • 37
0

Well, the point is that you have a tree structure that is mapped to the flat (row) structure of the DB.

You will need to handle this with a recursive delete method.

Your code should probably look something like:

function DeleteCategory(catId)
{
    GetChildCategoriesOf(catId)

    if (num_rows > 0)
    {
         foreach(ChildCategoryFound)
         {
              DeleteCategory(ChildCatId); //Recursive call
         }
    }

    Delete(catId) //Delete the parent as last action
}
Markus Deibel
  • 1,261
  • 20
  • 26