douyan8961 2015-11-04 18:08
浏览 28
已采纳

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

  • 写回答

1条回答 默认 最新

  • duan19780629 2015-11-05 05:20
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入