php MySQL 仅插入两个字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12670979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:00:46  来源:igfitidea点击:

MySQL INSERT two fields only

phpmysqlinsert

提问by Brandon

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!

有没有办法做到这一点?谢谢!

回答by Sjoerd

Yes, specify the column names after the table name:

是的,在表名后指定列名:

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

回答by Peter

I'd prefer

我更喜欢

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

回答by NET Experts

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

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

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

回答by Jelle De Laender

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

缺失的字段将具有该字段的默认值