简体   繁体   English

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

[英]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) 我想从表单和另一个表中插入数据,其中inv_id = "'.$inv.'"在同一表"history" ($inv是从表单输入的数据)(对不起,我的英语不好)

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: 历史记录表:

在此处输入图片说明

You could use a sub-query to generate the value for the condition field: 您可以使用子查询为condition字段生成值:

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 同样根据查询的格式,应该为查询使用准备好的语句,而不是将变量直接注入查询中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个表以及MYSQL和PHP中的表单插入数据 - Insert data from another table and also from form in MYSQL and PHP 我想将表单中的数据插入表中,然后从另一个表中选择另一个数据并使用PHP插入到第三张表中 - I want to insert a data from a form into a table, and select another data from another table and insert into third table using PHP PHP / SQL使用准备好的语句从表单插入数据并从另一个表插入数据 - PHP/SQL Insert data from form and insert data from another table using prepared statement PHP 将数据从一个表插入到另一个表 - PHP Insert data from one table to another 我想插入一个表中,从同一数据库中的另一个表中获取数据的PHP - i want to insert into a table, a data gotten from another table in the same database in php PHP MySQL使用where子句INSERT来自另一个表的数据,并将唯一值插入到相同的行中 - PHP MySQL INSERT data from another table using the where clause and insert unique values to the same rows 如何合并来自表单和来自三个mysql表的数据,以使用php将数据插入另一个表中? - How can I combine data from form and from three mysql tables to insert data in another table with php? 从db表1中选择数据并将其插入到php中的另一个db表2中 - Select data from db table 1 and insert it into another db table 2 in php 如何从一个表中获取数据并插入到php中的另一个表中 - how to fetch data from one table and insert into another table in php 如何使用php将一个表中的选定数据插入另一个表? - How to insert selected data from a table into another table using php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM