How to use two insert queries in same function in php - php

I have two insert queries in one table where two columns I am inserting from other table and one is I am inserting by own but in not working. Please help me.
INSERT INTO `g8_list_exec`(`g8_task_id`,`g8_tmpl_id`,`g8_chk_id`)
SELECT `g8_rec_id`,`g8_tmpl_uid` FROM `g8_list_add_task`
WHERE g8_tmpl_uid = 'oe_507','ik_862'
AND
INSERT INTO `g8_list_exec` (`g8_chk_id`) VALUES ('ik_862')

Try this
INSERT INTO g8_list_exec (g8_task_id,g8_tmpl_id,g8_chk_id)
SELECT g8_rec_id,g8_tmpl_uid,'ik_862'
FROM g8_list_add_task WHERE g8_tmpl_uid = 'oe_507';

use below query it will work for you
INSERT INTO g8_list_exec(g8_task_id,g8_tmpl_id,g8_chk_id) SELECT g8_rec_id,g8_tmpl_uid,'ik_862' FROM g8_list_add_task WHERE g8_tmpl_uid = e_507','ik_862'
value which you want to insert manually place in select query only after all columns.

Related

How to Insert new record in mysql db using two tables

I have a little problem. I've create a two tables 'kontrola' and 'naruszenie'. In kontrola table I have a foreign key relation to 'naruszenie' table. When I want to display all records belong to 'kontrola' table I use this:
$listakontroli = $connecting->query("SELECT k.id, k.podmiot, k.miasto, k.wszczeto, k.zakonczono, n.naruszenie FROM kontrola k INNER JOIN naruszenia n ON k.naruszenie_id=n.id");
And everything working properly.
How to create a query INSERT TO to add the new record using this 2 tables?
EDIT:
Now I have query like this but doesn't work
$dodajKontrole = "INSERT INTO kontrola (podmiot, miasto, wszczeto, zakonczono, naruszenie_id) VALUES ('$nowyPodmiot', '$noweMiasto', '$datawszczecia', '$datazakonczenia', '$nowenaruszenie')";
Try to use a mysql TRIGGER if you want to do it in mySQL level
CREATE TRIGGER insert_into_naruszenia AFTER INSERT ON `kontrola`
FOR EACH ROW
BEGIN
INSERT INTO naruszenia (naruszenie) VALUES (NEW.id)
END;
INSERT INTO kontrola (....) VALUES (....);
I didn't test it but is should do the job.
If you want to use it inside php code you always have the LAST_INSERT_ID() that you can use.
INSERT INTO kontrola (....) VALUES (....);
INSERT INTO naruszenie (id) VALUES (LAST_INSERT_ID());

PHP - Does it need to fetch_array when select then insert

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.

Is it possible to insert data in two different table using single mysql query without transaction?

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

mysql is it possible to do insert into insert?

what am doing is clonning an old database to new one with different structure but should have same old data
is it possible to do an insert into insert that return id of the insert query
INSERT INTO tab1 (nom,id_2) VALUES
("jack",(INSERT INTO tab2 (pass) VALUES ("1234")));
in INSERT INTO tab2 (pass) VALUES ("1234") i want to get the id of the insert
but no luck!! (i want it in one sql query (i already know about mysql_insert_id) )
thanks in advance
You cant do this on a single query - use mysql_insert_id() to get the last inserted id for example :
mysql_query('INSERT INTO tab2 (pass) VALUES ("1234")');
$lastid = mysql_insert_id();
mysql_query('INSERT INTO tab1 (nom,id_2) VALUES ("jack",'.$lastid.')');
insert id can be retrieved in mysql as well, so here is a version with no PHP involved (it however performs 2 queries)
INSERT INTO tab2 (pass) VALUES ("1234");
INSERT INTO tab1 (nom,id_2) VALUES("jack",LAST_INSERT_ID());
Lock the table, insert, select max(id), unlock table. This is the only way outside creating your own sequence tables like in postgres if you are adverse for some reason to the last insert id functions. This approach however is going to be like pouring tar into a blender if your table is moderate to high writes.
You can't do this as a single insert because inserts are atomic that is, the ID isn't determined until the statement completes.
Wrap both statements in a transaction and you will get your ID, and atomicity.

How do I insert data while updating?

There are two tables called table1 and table2
both table has got same index. when updating table1 and table2 if table2 hasnt got a data and table1 has got the same then it should insert that data to table2. so that both tables got the data and is updated. how can I do this??
create a trigger on table 1 to query for the new row in table 2. if not found, insert.
showing code would help, but one option is to use REPLACE() it will add if needed to replace otherwise, if you really need two identical tables you should look in to replication and stored procedures

Categories