A very very bad design i must say.
I don’t know how the data gets insert because that sql statement should throw an error.
As far your problem, what is foreach doing on insert statement? You can send only 1 value at a time. Foreach should overwrite the variables each time….
A big mess. You should simplify the decision. But none the less, remove that foreeach. It should resolve your problem.