Dynamically mapping form input to SQL columns in the way intended here is a terrible idea from a security standpoint. Anyone will be able to dump any data into any column by constructing a matching POST request.
Furthermore, by interpolating form values directly into your SQL string you are vulnerable to SQL injection. Anyone can submit a fake POST request and execute arbitrary SQL that will expose or damage any table in your database.
With that warmingwarning being given, what you want is to iterate over the keys in the first record using PHP's foreach
http://www.php.net/manual/en/control-structures.foreach.php
foreach ($main_record as $column_name => $column_value) { ... } Inside the loop, you will have to build up two strings in each iteration. The first one contains the column names (the comma seperated list after the table name). The second one contains the values.
After that, you will have to get the ID of the last inserted record, PDO has a function for that. Then you'll eachhave to repeat this technique for each of the associated records.
If you want a better solution, consider using a framework like Laravel, which includes an Object-Relational Mapper library called Eloquent ORM. It will make these kind of operations simpler and safer to program.