0

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

2
  • you want to update the condition of the table istoric with the value of status echipament? Commented Nov 4, 2015 at 19:07
  • If you have trouble using the english stackoverflow or you find it difficult to explain and understand, you can use the portuguese. Also, regarding your query, and depending on how the database is implemented, you could consider making the condition field as a foreign key for the echipament table, instead of what appears to be a text field. Commented Nov 5, 2015 at 9:43

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.