PHP:从表单和同一个表中的另一个表中插入数据
I want to insert data from a form and another table where inv_id = "'.$inv.'"
are in the same table "history"
($inv
is data input from form) (sorry for my bad english)
Actually my query:
$query2 = "INSERT INTO istoric SET id_user = '".$user."',id_equipment = '".$nr_inv."',start = '".$startdate."',end = '".$enddate."',comment = '".$comment."'";
$id = "INSERT INTO `istoric`(`condition`) SELECT `status` FROM `echipament`WHERE `nr_inventar` = '".$nr_inv."'";
How to combine two query? Now this query insert data in two different rows.
History table:
我想从表单和另一个表中插入数据,其中 实际上是我的查询: p>
如何组合两个查询? 现在,此查询将数据插入两个不同的行。 p>
历史记录表: p>
inv_id =“'。$ inv。' “ code>在同一个表
”history“ code>
($ inv code>是从表单输入的数据)(抱歉我的英文不好) p>
\ n
$ query2 =“INSERT INTO istoric SET id_user ='”。$ user。“',id_equipment ='”。$ nr_inv。“ ',start ='“。$ startdate。”',end ='“。$ enddate。”',comment ='“。$ comment。”'“;
$ id =“INSERT INTO`istoric`(`condition`)SELECT`status FROM FROM echipament`WHERE`nr_inventar` ='”。$ nr_inv。“'”;
code> pre> \ n
You could use a sub-query to generate the value for the condition
field:
INSERT INTO
`istoric`
SET
`id_user` = '".$user."',
`id_equipment` = '".$nr_inv."',
`start` = '".$startdate."',
`end` = '".$enddate."',
`comment` = '".$comment."',
`condition` = (
SELECT
`status`
FROM
`echipament`
WHERE
`nr_inventar` = '".$nr_inv."'
);
Also based on how you've formatted your query, you should used prepared statements for your queries instead of injecting the variables directly into your query