PHP MYSQL - Insert into without using column names but with autoincrement field

前端 未结 4 1785
無奈伤痛
無奈伤痛 2020-12-01 07:04

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

I\'d like to do something like this:

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


        
相关标签:
4条回答
  • 2020-12-01 07:43

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

    Advise if above information is wrong.

    0 讨论(0)
  • 2020-12-01 07:55

    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)
  • 2020-12-01 08:06

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

    INSERT INTO tblname VALUES (NULL, ... 32 Fields ... )
    
    0 讨论(0)
  • 2020-12-01 08:07

    Insert NULL into the auto-increment field.

    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.

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

    0 讨论(0)
提交回复
热议问题