I know very little of PHP and MySQL, but i would say that you could add a new column in your database with the name ID

Then you would match Action-ID=1 Drama-ID=" etc..

Then you could make an 'until' routine until there are no more ids