Php – One query to insert multiple rows with multiple columns

insertmultiple-columnsmysqlphp

Let's say I want to insert this data:

Row 1: People = '40', Places = '15'
Row 2: People = '5', Places = '10'

I understand that this is how you'd execute the above:

mysql_query("INSERT INTO mytable(`People`, `Places`) 
VALUES ('40', '15'),('5', '10')");

But what if I wanted to insert into more than two columns with a single query? What if the data to be inserted was like this:

Row 1: People = '40', Places = '15'
Row 2: People = '5', Places = '10'
Row 3: Things = '140', Ideas = '20'
Row 4: People = '10', Things = '5', Ideas = '13'

I can't seem to find a question like this anywhere else.

Best Solution

Leave the columns you don't wanto to fill with null

INSERT INTO mytable(`People`, `Places`, Things, Ideas) 
VALUES ('40', '15', null, null),(null, null, 100, 20)