I am introducing my own blogging system to better understand the object-oriented side of PHP, mySQL and jQuery AJAX. I have a many-to-many relationship between Post_T and Tag_T, including the associative PostTag_T object. When creating a message, I added functions that will check if this tag already exists, as well as add a new tag and match, or just add a match to an existing tag.
The failure I noticed is that when deleting Mail, the tag string in Tag_T still exists while the mapping in PostTag_T is deleted (set to ON DELETE CASCADE). How can I remove this tag if it is not used by any other mail? I have a message id when deleting a message.
Useful logic
Using the message identifier from the deletion, delete from tag_t, where in posttag_t pID = post id AND no other messages (pID) in posttag_t use the same tag identifier .
delete from tag_t where tID = (select tID from posttag_t where pID='post id') AND..
For your convenience, here is a visual representation.
Created post line in Post_T:

Display message and tag in PostTag_T:

Tag line created in Tag_T:

On removal: using AJAX POST with data calling the php file in the url (assuming I might need to add a query that looks for tables ..)

source share