As others have pointed out, it may be your schema but your code has couple of red flags in that makes me think it may be in your code:
- connecting to the database inside a loop - more than likely *not* what you want
- creating string via loop and also inserting in loop - that's going to lead to insert foo, insert foobar, insert foobarbaz, insert foobarbazqux, etc
- code formatting leaves a lot to be desired (if it's easier to read, it's easier to debug)
but maybe that's just because it's a snippet posted here for help.