Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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.

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

Suggestions?

like image 685
Paulo Bueno Avatar asked Dec 09 '09 02:12

Paulo Bueno


People also ask

Can you insert into auto increment field MySQL?

Syntax for MySQLMySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature. By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record. VALUES ('Lars','Monsen'); The SQL statement above would insert a new record into the "Persons" table.

How to insert data INTO a particular row IN MySQL?

When inserting a single row into the MySQL table, the syntax is as follows: INSERT INTO table_name(column_1,column_2,column_3) VALUES (value_1,value_2,value_3); In the INSERT INTO query, you should specify the following information: table_name : A MySQL table to which you want to add a new row.

When inserting data in a table do you always have to specify a list of all column names you are inserting values for no yes?

As long as you have the right number of columns in your INSERT statement, and as long as all the values except KEYBOARD are some numeric data type, and as long as you have suitable permissions, this should work. INSERT INTO INVOICE VALUES( 1,1,'KEYBOARD',1,15,5,75); SQL requires single quotes around text values.

How insert IGNORE works?

Insert Ignore statement in MySQL has a special feature that ignores the invalid rows whenever we are inserting single or multiple rows into a table. We can understand it with the following explanation, where a table contains a primary key column. The primary key column cannot stores duplicate values into a table.


2 Answers

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

INSERT INTO tblname VALUES (NULL, ... 32 Fields ... ) 
like image 154
Doug Neiner Avatar answered Sep 23 '22 21:09

Doug Neiner


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.

like image 20
gahooa Avatar answered Sep 23 '22 21:09

gahooa