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?
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.
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.
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.
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.
Just use NULL
as your first value, the autoincrement
field will still work as expected:
INSERT INTO tblname VALUES (NULL, ... 32 Fields ... )
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With