Reputation: 2171
I have a table with quite a few columns. The total number of columns is not yet specified, and will change on a regular basis.
In my insert query, I only need to put two values into the table. All other values will be ' '. is there a way to only specify the first fields, without having to include '','','',''...? Please see below for example:
I would like to have this:
$query = mysql_query("INSERT INTO table VALUES('','$id')");
Rather than this:
$query = mysql_query("INSERT INTO table VALUES('','$id','','','','','',''......and on and on...)");
Is there a way to do this? Thanks!
Upvotes: 17
Views: 33776
Reputation: 1536
INSERT INTO table_name (column1, column2) VALUES (value1, value2)
http://www.w3schools.com/php/php_mysql_insert.asp
Upvotes: 6
Reputation: 1798
I'd prefer
INSERT INTO table SET columnA = 'valueA', columnB = 'valueB'
Upvotes: 8
Reputation: 577
Just define the fields you will insert,
eg:
INSERT INTO table (fieldA, fieldB) VALUES('','$id')
the missing fields will have the default value for that field
Upvotes: 3
Reputation: 75588
Yes, specify the column names after the table name:
INSERT INTO table (column1, column2) VALUES ('','$id')
Upvotes: 30