MySQL insert mutiple rows based on SELECT query result
One thing you have to know is that the number of columns returned by your query must match the number of columns you want to insert into
"INSERT INTO NewTable(plan_name, plan_time) SELECT cp.plan_name, cp.plan_time FROM courses c INNER JOIN course_to_plan cpl ON cpl.course_id = c.course_id INNER JOIN courseplans cp ON cp.plan_id = cpl.plan_id WHERE cpl.course_id = '$course_id'"
Warning: watch out for sql injection through
$course_id
.
Note that I specified 2 columns in my INSERT statement because the SELECT query return 2 columns
If the number of columns in your table matches the number of columns returned by the query exactly, then you do not need to specify the columns.
Not sure about the php code but if you change your mysql query to an insert/select it should work:
INSERT INTO newtable(plan_name, plan_time) SELECT cp.plan_name, cp.plan_time FROM courses c INNER JOIN course_to_plan cpl ON cpl.course_id = c.course_id INNER JOIN courseplans cp ON cp.plan_id = cpl.plan_id WHERE cpl.course_id = '$course_id'