Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL INSERT two fields only

Tags:

php

mysql

insert

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!

like image 714
Brandon Avatar asked Oct 01 '12 09:10

Brandon


People also ask

Can you insert multiple values in MySQL?

INSERT statements that use VALUES syntax can insert multiple rows. To do this, include multiple lists of comma-separated column values, with lists enclosed within parentheses and separated by commas.

How can I add values to one column in a table in MySQL?

First, you must specify the name of the table. After that, in parenthesis, you must specify the column name of the table, and columns must be separated by a comma. The values that you want to insert must be inside the parenthesis, and it must be followed by the VALUES clause.

How do I add a column between two columns in MySQL?

Syntax. The syntax to add a column in a table in MySQL (using the ALTER TABLE statement) is: ALTER TABLE table_name ADD new_column_name column_definition [ FIRST | AFTER column_name ]; table_name.

How can I store multiple values in a single column in MySQL using PHP?

Insert Mutliple values in a single Field: For inserting the flat no's in a single field, use implode and the values separated by comma. For retrieve the values from database use, explode to separate out all the values.. For Retrieve the values using $i=explode(",",$r);


2 Answers

Yes, specify the column names after the table name:

INSERT INTO table (column1, column2) VALUES ('','$id')
like image 92
Sjoerd Avatar answered Sep 30 '22 01:09

Sjoerd


I'd prefer

INSERT INTO table SET columnA = 'valueA', columnB = 'valueB'
like image 39
Peter Avatar answered Sep 30 '22 03:09

Peter