This can work if the values in the temporary table are unique. The idea is to split the inserts into two steps, one into the primary table and the rest into the secondary table:
insert into primary( . . .)
select . . .
from temp;
insert into secondary(primaryid, . . . )
select p.PrimaryId, t.col . . .
from temp t join
primary p
on t.col1 = p.col1 and . . .;
There are some caveats. For instance, you will need more complex logic to handle joins. And, it assumes that each set of primary columns in the temporary table is unique.
The most common approach is a loop in a stored procedure or application code that uses LAST_INSERT_ID
.