I'm attempting to do a "bulk" insert with the following query:
INSERT INTO `order_status_histories` VALUES ('3602','52efabe9-5f8c-4512-a994-3227c63dd20e','1','','Order recieved','2014-02-03 16:47:05','2014-02-03 16:47:05'),('3603','52eff713-54fc-4be0-9389-68d5c63dd20e','1','','Order recieved','2014-02-03 22:07:47','2014-02-03 22:07:47'),('3604','52effd1a-bc14-4095-97fd-6d46c63dd20e','1','','Order recieved','2014-02-03 22:33:30','2014-02-03 22:33:30')
However, it is failing, because the first column in the table is recordID, and does not exist in the insert statement.
How can I get around this issue?
When you use
INSERT INTO `order_status_histories` VALUES (....)
you need to pass each value that the table structure requires, in the correct order. You can omit fields from the values if they have a default value in the table structure.
Most of the time, it's better to explicitly list the fields that you want to insert to - if nothing else, it means that if the table structure changes, you won't need to re-write your SQL statements. Try changing your SQL statement so it's structured as:
INSERT INTO `order_status_histories` (field1, field2, ....) VALUES ('value1', 'value2', ....)
Related
Hello I'm writing PHP and mysql. If I want to select then insert to database, I will
step1.Select from table
step2. mysqli_fetch_array and contain to variable such as $foo
step3. INSERT INTO MyTable (firstname)
VALUES ('$foo')"
Is it possible that I select then insert without fetch from database?
If you want to do this via mysql you can use
INSERT INTO Table2(field1, field2)
SELECT old_field1 as field1, old_field2 as field2 FROM Table1
Just be sure that you match fieldnames with "as".
Its a very simple concept,
mysqli_fetch_array()
returns an array and you cannot directly store an array into a column of a table in database. Either you have to map with the table column or convert it into json to store it into table column.
I have a 'location' table and a 'location_detail'[for inserting different language data] table. 'location_detail' contains FK of location table
I need to enter multiple location at a time. So what I am doing is:
run a 'for' loop inside that first I enter data into 'location' table get the loc_id then insert into location_detail table[Here in 'location_detail' table if more than one language present, again I want to run the query multiple times].
So if I want to add 3 locations -> Outer 'for' loop will run 3 times
total no of query exec. is 6 [If more than one language is present this will multiple]
==>My aim is to insert all 3(say) locations into 'location' table using multiple insert in a single statement and get all 3 last_insert_ids.
==>Next I can run single statement multiple insert query for adding into 'location_details' table
Here, how will I get this last_insert_ids in an array?
I'll do this in the same transaction/connection :
INSERT INTO location (col1, col2) VALUES (val1a, val2a);
SET #string_ids = LAST_INSERT_ID();
INSERT INTO location (col1, col2) VALUES (val1b, val2b);
SET #string_ids = CONCAT(#string_ids , ",", LAST_INSERT_ID());
INSERT INTO location (col1, col2) VALUES (val1c, val2c);
SET #string_ids = CONCAT(#string_ids , ",", LAST_INSERT_ID());
SELECT #string_ids ;
Then in php, I would explode this variable :
$array_ids = explode (",", $string_ids ).
Then build your request with php :
INSERT INTO location_detail(id, fk_id) VALUES
//foreach loop
(val_id1b, $array_ids[$i] )
(val_id2b, $array_ids[$i] )
(val_id3b, $array_ids[$i] )
I haven't tried the #array_ids but it should work.
Look at this link for more help if you need it.
I hope it fits your needs.
You can use transaction and get last_insert_id and calculate previous id's relate to your AUTO_INCREMENT settings.
Important
If you insert multiple rows using a single INSERT statement, LAST_INSERT_ID() returns the value generated for the first inserted row only. The reason for this is to make it possible to reproduce easily the same INSERT statement against some other server.
from Last Insert ID documentation.
I need to insert same data to my MySQL table without having PHP loop. The reason why I'm doing this is that because I have a column with Auto_Increment feature and that column associates with other table.
So, I just need to insert some exactly same data and it's multiple rows (dynamic) but by using single INSERT syntax below :
INSERT INTO outbox_multipart (TextDecoded) VALUES ('$SMSMessage')
how to have this single INSERT syntax, but produce n number of rows?
You can do this:
INSERT INTO outbox_multipart (TextDecoded) VALUES ('$SMSMessage')
, ('$SMSMessage2'), ('$SMSMessage3'), ('$SMSMessage4');
INSERT INTO outbox_multipart (TextDecoded) VALUES ('$SMSMessage'),('$SMSMessage')
,('$SMSMessage'),('$SMSMessage')
if done dynamically,
$n=5;
for ($i=0;$i<$n;$i++){$values.="('$SMSMessage'),";}
$values=substr($values,0,-1);
And the SQL be:
INSERT INTO outbox_multipart (TextDecoded) VALUES $values
mysql_query("INSERT INTO `table`(`this`) VALUES (`that`); INSERT INTO `table`(`this`) VALUES (`that`);");
try something like this
$sql = mysql_query("INSERT INTO outbox_multipart (TextDecoded) VALUES ('$SMSMessage')
, ('$SMSMessage'),('$SMSMessage'),('$SMSMessage')");
Also, just so you know, mysql_* have been deprecated. So, try to use mysqli_* or PDO for querying.
suppose you want 3 insertions simply do like this:
INSERT INTO outbox_multipart (TextDecoded) VALUES ('$SMSMessage'),('$SMSMessage'),('$SMSMessage');
I am trying to make a query that will insert multiple values from my form into a single row in my table.
$q2="INSERT INTO tbl_Answer('Answer')VALUES ('$A1'),('$A2'),('$A3'),('$A4'),('$A5')";
Everything I have found tells me this should work using PHP with a MySQl database. Any ideas if I have done something wrong with the syntax or where my issue is? Thanks
INSERT INTO tbl_Answer (Answer) VALUES ('$A1'),('$A2'),('$A3'),('$A4'),('$A5')
Is the same as saying:
INSERT INTO tbl_Answer (Answer) VALUES ('$A1')
INSERT INTO tbl_Answer (Answer) VALUES ('$A2')
INSERT INTO tbl_Answer (Answer) VALUES ('$A3')
INSERT INTO tbl_Answer (Answer) VALUES ('$A4')
INSERT INTO tbl_Answer (Answer) VALUES ('$A5')
What exactly are you looking for?
Here is a demo of your original query working: http://sqlfiddle.com/#!2/e20fc/1
Looks like the value of $A1-$A5 is throwing the query off. Are you receiving any error?
We might need more information however a couple of things:
If you are using mysql in unix based system tables are case sensitive.
Second, should you use ` instead of ' on the name of your columns?(and i would say they are both innecesary) like:
INSERT INTO tbl_Answer(`Answer`) or even better INSERT INTO tbl_Answer(Answer)
You need more fields in your table. You should have a field for each of your variables.
You have a minor problem with your query syntax.
$q2 = "INSERT INTO tbl_Answer
('A1', 'A2', 'A3', 'A4', 'A5')
VALUES
('$A1', '$A2', '$A3', '$A4', '$A5');";
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?
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.
Use NULL or 0 to insert an auto-incremented value as shown below:
-- Here
INSERT INTO tblname VALUES (NULL, ... 32 Fields ... )
-- Here
INSERT INTO tblname VALUES (0, ... 32 Fields ... )
We should omit any column values when we try without column name in insert query,
Advise if above information is wrong.
Here's a simple shortcut that I've used:
$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.')';
Please note that this code would be vulnerable to SQL Injection and should be modified to accommodate PDO's, but I felt this simplified script would more directly answer the question with regards to the originally posted code.