Brandon
Brandon

Reputation: 2171

MySQL INSERT two fields only

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

Answers (4)

NET Experts
NET Experts

Reputation: 1536

INSERT INTO table_name (column1, column2) VALUES (value1, value2)

http://www.w3schools.com/php/php_mysql_insert.asp

Upvotes: 6

Peter
Peter

Reputation: 1798

I'd prefer

INSERT INTO table SET columnA = 'valueA', columnB = 'valueB'

Upvotes: 8

Jelle De Laender
Jelle De Laender

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

Sjoerd
Sjoerd

Reputation: 75588

Yes, specify the column names after the table name:

INSERT INTO table (column1, column2) VALUES ('','$id')

Upvotes: 30

Related Questions