PHP:从表单和同一个表中的另一个表中插入数据

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:

enter image description here

我想从表单和另一个表中插入数据,其中 inv_id =“'。$ inv。' “ code>在同一个表”history“ code> ($ inv code>是从表单输入的数据)(抱歉我的英文不好) p> \ n

实际上是我的查询: p>

  $ 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 
 

如何组合两个查询? 现在,此查询将数据插入两个不同的行。 p>

历史记录表: p>

p> div>

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