php --without-mysql,PHP MYSQL -插入時不使用列名,而是使用自動遞增字段

I need to insert a long row with 32 fields into a MySQL table.

我需要在MySQL表中插入32個字段的長行。

I'd like to do something like this:

我想做這樣的事情:

$sql="insert into tblname values (... 32 fields ...)";

Obviously it works fine if the fields are in the same order as the MySQL table fields. But, my table has an auto-increment id as it's first field.

顯然,如果字段的順序與MySQL表字段相同,那么它可以正常工作。但是,我的表有一個作為第一個字段的自動遞增id。

What I want is to fill in all table names but the first (id) one.

我想要填充除了第一個(id)以外的所有表名。

Suggestions?

建議嗎?

4 个解决方案

#1

66

Just use NULL as your first value, the autoincrement field will still work as expected:

只使用NULL作為第一個值,自動遞增字段仍將按預期工作:

INSERT INTO tblname VALUES (NULL, ... 32 Fields ... )

#2

12

Insert NULL into the auto-increment field.

在自動遞增字段中插入NULL。

I recommend that unless this is a hack script, you use field names. The rationale is that your code will break if you ever add a field to the table or change their order.

我建議您使用字段名,除非這是一個hack腳本。基本原理是,如果向表中添加字段或更改它們的順序,您的代碼將會崩潰。

Instead, be explicit with field names, and it will go much better in the future.

相反,要用字段名顯式表示,這樣將來會更好。

#3

-1

We should omit any column values when we try without column name in insert query,

當我們嘗試在insert query中不使用列名時,我們應該省略任何列值,

Advise if above information is wrong.

如果以上信息是錯誤的,請告知。

#4

-3

Here's a great shortcut that I've used (courtesy of a friend who wrote it for me)

這是我用過的一個很好的捷徑(感謝一個為我寫的朋友)

$fieldlist=$vallist='';

foreach ($_POST as $key => $value) {

$fieldlist.=$key.',';

$vallist.='\''.urlencode($value).'\','; }

$fieldlist=substr($fieldlist, 0, -1);

$vallist=substr($vallist, 0, -1);

$sql='INSERT INTO customer_info ('.$fieldlist.') VALUES ('.$vallist.')';

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值