PHP: Insert data from form and another table in the same table

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

Upvotes: 0

Views: 652

Answers (1)

Ben Rowe
Ben Rowe

Reputation: 28691

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

Upvotes: 1

Related Questions