Ok, I was able to solve the problem.It doesn't really need any coding at all.
Just for the benefit of others, here's a general description of what i did.
I build two tables in SQL, the two tables should have primary keys in order for submit to work.
I then created an infopath form based on the database. The two tables are linked as one-to-many ...