views:

935

answers:

2

Hi guys,

I've to insert a long form with 32 fields into a mysql table.

i'd like to do that something like this:

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

Obviosly it works fine if the fiedls were in the same order as the mysql table fields. But, my table has as first field an id (auto-increment).

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

Suggestions?

+4  A: 

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.

gahooa
+6  A: 

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

INSERT INTO tblname VALUES (NULL, ... 32 Fields ... )
Doug Neiner
Very good.I've also found an alternate solution as follow:$resultx = mysql_query( "SHOW TABLE STATUS LIKE 'diretorio'");$auto_incr_val = mysql_result($resultx, 0, 'Auto_increment');
Paulo Bueno
@Paulo: you have no idea what trouble you may open yourself up to by doing that. Use NULL - it's the way MySQL designed it to work.
gahooa