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
}
Related
INSERT INTO service (col1, col2, col3) VALUES ('test', '2001-01-01', 'test');
SET #last_id = LAST_INSERT_ID();
INSERT INTO artikel_service (service_id) VALUES (#last_id);
The above query works great in phpmyadmin but not in PHP (XAMPP), why is that?
You need to get last inserted id with this query separately: SHOW TABLE STATUS LIKE 'service' and then in the result of above query you will get last inserted id (minus(-) 1) value in column 'Auto_increment'.
I'm quite confused with sql INSERT INTO. Do I need to put JOIN? or INNER JOIN when I'm going to put into different table?
$sqlinsert = "INSERT INTO forum_topic (topic_title, topic_message, thread_id)
VALUES
('$topic','$message','$thread_id')";
I know this code is works for only one table but not for two. However, what I want is $message into post_message in other table.
In forum_topic table, there is post_id, post_message, date, and topic_id.
But there is forum_post table where I want to put $message into post_message in forum_post.
So, in forum_post table, there is post_id, post_message, date, and topic_id.
So do I need to JOIN or INNER JOIN? I don't know which one I should code them.
PS : I know the question is quite hard for you to understand. How I wish i can put image but I don't have 10 reputations, so i decided to do like this.
You cannot insert into multiple tables with one query, but you can use transactions to make sure that the operation is atomic.
BEGIN;
Your queries here
COMMIT;
When you need to execute several interdepending queries, some of them might result in error. So to keep the database consistent you would like to execute them as all-or-none (You can read about ACID).
MySQL also supports transactions but only with some of its storage engines (e.g. InnoDB, but not MyISAM).
The pseudo-code of usage looks like this.
start transaction
try {
execute query 1
execute query 2
...
commit transaction
catch {
rollback transaction
}
If you are using PDO, look at these functions.
In case of mysql_query, you can use
mysql_query("START TRANSACTION");
mysql_query("COMMIT");
mysql_query("ROLLBACK");
You don't use joins. You use separate queries for each table.
insert into table1
(field1, field2, etc)
values
(value1, value2, etc);
insert into table2
(field1, field2, etc)
values
(value1, value2, etc);
To insert into multiple tables you must use multiple queries.
Example:
$sqlinsert1 = "INSERT INTO forum_topic1 (topic_title1, topic_message1, thread_id1)
VALUES
('$topic1','$message1','$thread_id1')";
$sqlinsert2 = "INSERT INTO forum_topic2 (topic_title2, topic_message2, thread_id2)
VALUES
('$topic2','$message2','$thread_id2')";
Join are used for Selecting queries.
Example for a Join:
$sqlselect = "SELECT * FROM forum_topic
Join table2 on thread_id = table2id
WHERE topic_title = $topic";
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');";