Take the 2-minute tour ×

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 fields were in the same order as the mysql table fields. But, my table has as first field an id (auto-increment).

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


share|improve this question

4 Answers 4

up vote 45 down vote accepted

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

INSERT INTO tblname VALUES (NULL, ... 32 Fields ... )
share|improve this answer
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 Dec 9 '09 at 3:04
@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 Dec 11 '09 at 22:52

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.

share|improve this answer

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

Advise if above information is wrong.

share|improve this answer

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

 $vallist.='\''.urlencode($value).'\','; }
$fieldlist=substr($fieldlist, 0, -1);
$vallist=substr($vallist, 0, -1);
$sql='INSERT INTO customer_info ('.$fieldlist.') VALUES ('.$vallist.')'; 
share|improve this answer
This code is vulnerable to SQL Injection. Read more here please: stackoverflow.com/questions/11939226/… –  Ilia Rostovtsev Aug 26 '12 at 9:29
@IliaRostovtsev - While I agree with you and have switched over to PDO's in order to avoid SQL injection, I feel there's a benefit to answering the question as it pertains to the posted code –  Chaya Cooper Nov 23 at 1:49
Yes, probably. It also worth mentioning aboug the possible problems. (I didn't downvote, just in case) –  Ilia Rostovtsev Nov 23 at 7:45

Your Answer


