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.')';