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');";
Related
I know this is possible using transaction but I want to do it using single mysql query.
this is which common format transaction
START TRANSACTION;
SELECT #A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=#A WHERE type=1;
COMMIT;
but I need to know a single mysql query is possible?
Normally, you can not do this in MySQL. But you can do this using concat query.
INSERT INTO your_table
(value1,value2,value3)
VALUES
(a,b,c),
(d,e,f),
(i,j,k);
But this is not your question answer. so Your question answer is NO. MYSQL is not support it still now.
No, it can't be done in single statement like
insert into table1,table2
either you do separately like
insert into table1 ...
insert into table2 ...
(OR)
Wrap the insert statements in stored procedure and call that procedure like
create procedure sp_insert_multiple
as
begin
insert into table1 ...
insert into table2 ...
end
Call the SP
exec sp_insert_multiple
You can't do this. However, you can use a transaction and have both of them be contained within one transaction.
START TRANSACTION;
INSERT INTO table_1 VALUES ('1','2','3');
INSERT INTO table_2 VALUES ('one','two','three');
COMMIT;
See the following rule
Normally it is not possible to insert multiple table in single query. you can insert multiple row in a single table . like as
INSERT INTO tbl_test
(a1,a2,a3)
VALUES
(1,2,3),
(4,5,6),
(7,8,9);
you can do this in Oracle
by using procedure you can insert
create procedure insert_query
as
begin
insert into tbl_test1(a1,a2,a3) VALUES (1,2,3)
insert into tbl_test2 (b1,b2,b3) VALUES (1,2,3)
end
you can do that like coz
MySQL doesn't support multi-table insertion in a single INSERT statement.
INSERT INTO NAMES VALUES(...)
INSERT INTO PHONES VALUES(...)
here is a link of detail answer..
sql - insert into multiple tables in one query
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', ....)
another query gone wrong!
I am trying to INSERT INTO anretOrders (order, orderNumber) VALUES ('test', 15)
Now, the anretOrders table has 3 columns, but one is the id so i am leaving that out since it auto-increments. The other is "order" which is a text column, and orderNumber which is an int.
I cannot, for the life of me, figure out what is wrong here? what am i missing?
escape the field names since order is a keyword in mysql
INSERT INTO anretOrders (`order`, `orderNumber`) VALUES ('test', 15)
order is a Reserved Words
So try like this
create table anretOrders (`order` varchar(20),orderNumber int);
INSERT INTO anretOrders (`order`, `orderNumber`) VALUES ('test', 15)
Sql FIDDLE
I need to insert data into two different tables in the same database using one button, but can't work out how.
My two insert commands look like this:
$insertSQL1 = sprintf("INSERT INTO table2...blah blah blah..."); // this is a whole load of info from the form
$insertSQL2 = ('INSERT INTO table2...blah blah blah...'); // this is some more data, including an array
I have tried putting them into a "BEGIN WORK... COMMIT WORK" function, as follows:
$insertSQL = "BEGIN WORK;
".$insertSQL1.";
".$insertSQL2.";
COMMIT WORK;";
...but I get always get the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO...'
I can't for the life of me work out what the actual syntax error is. Any suggestions on a better way of doing this?
Use can't execute multiple queries in one call (with a normal query call). Use this syntax
insert into table2 (col1, col2, col3)
values (1, 2, 3),
(4, 5, 6)
to insert multiple records with one query.
You will have to use two insert functions for that:
$query1 = "INSERT INTO table1 (col1, col2) values (1,2)";
$query2 = "INSERT INTO table2 (col1, col2) values (1,2)";
mysql_query($query1);
mysql_query($query2);
I used mysql_query for simplicity, you have to use your own DB functions for that of course. It can still be done with one click, but you need to use 2 SQL statements. Just do this:
if ($clicked==true){
//insert queries
}
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');