I was able to fix it. I don't know exaclty what was causing the error, I made a few improvements in the code and it worked.

Thanks for the heads up joker_josue
edit: By mistake I gave you merit with FeeBuddy lol
I was logged with the wrong user
It's not every day that you receive merits from FeeBuddy.

This thing about computing, sometimes it is enough to change a point, for everything to work differently. In turn, this is not always clear enough to understand why certain things happen.
But, everything is already working and I'm glad I helped in some way.