MySQL INSERT for only two fields

I have a table with multiple columns. The total number of columns has not yet been indicated and will be changed on a regular basis.

In my insert request, I only need to put two values ​​in the table. All other values ​​will be ". Is there a way to specify only the first fields without the need to include '', '', '', '' ...? See below, for example:

I would like to have this:

$query = mysql_query("INSERT INTO table VALUES('','$id')"); 

Instead of this:

 $query = mysql_query("INSERT INTO table VALUES('','$id','','','','','',''......and on and on...)"); 

Is there any way to do this? Thanks!

+4
source share
4 answers

Yes, provide the column names after the table name:

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

I would prefer

 INSERT INTO table SET columnA = 'valueA', columnB = 'valueB' 
+6
source

Just specify the fields you insert,

eg:

 INSERT INTO table (fieldA, fieldB) VALUES('','$id') 

missing fields will have a default value for this field

+1
source
 INSERT INTO table_name (column1, column2) VALUES (value1, value2) 

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

+1
source

Source: https://habr.com/ru/post/1437141/


All Articles