dongzi9196 2017-05-20 04:33
浏览 19

如何使用php中具有一对二关系的sql语句编辑两个表

I have two tables having one to many relationship.

data is being properly entered in both tables at one time, but now I want to edit both tables at one time and I m confused that how to update this.

I'm using parent table primary key to edit both tables.

When I update one record, the others records on same primary key also updated in child table.

when I update by using child table primary key to update tables then I have to update related fields also.

I do not know what to do, please help me.

These are my two tables and the query is :

mysqli_query($conn, "update new_invoice inner join new_item ON new_invoice.invoice_id = new_item.invoice_id_fk
  SET 
  new_invoice.c_name='$c_name',
  new_invoice.c_address='$c_address',
  new_invoice.invoice_date = '$invoice_date',
  new_invoice.amount = '$amount',
  new_item.item_name  = '$item_name',
  new_item.detail = '$detail',
  new_item.quantity = '$quantity',
  new_item.unit_cost = '$unit_cost',
  new_item.discount = '$discount',
  new_item.price = '$price',
  new_invoice.confirmation = '$confirmation'

where new_invoice.invoice_id='$invoice_id'   

");

I'm working on invoice system.

I want to update data in both tables at one time but how to do this.

Actually I'm new to stack overflow so please tell me how to answer your question.

can I answer in comment ?

I do not know the answer of my question.

My mean is this: How can I answer you if you ask me something about my question?

  • 写回答

1条回答 默认 最新

  • drtkyykai004574380 2017-05-20 07:16
    关注

    You will need to identify the row in the new_item table that needs to be updated, at the moment it's updating all rows for this invoice...

    mysqli_query($conn, "update new_invoice inner join new_item ON new_invoice.invoice_id = new_item.invoice_id_fk
      SET 
      new_invoice.c_name='$c_name',
      new_invoice.c_address='$c_address',
      new_invoice.invoice_date = '$invoice_date',
    new_invoice.amount = '$amount',
    new_item.item_name  = '$item_name',
    new_item.detail = '$detail',
    new_item.quantity = '$quantity',
    new_item.unit_cost = '$unit_cost',
    new_item.discount = '$discount',
    new_item.price = '$price',
    new_invoice.confirmation = '$confirmation'
    
    where new_invoice.invoice_id='$invoice_id'
        and new_item.line_no = $ine_no
    
    ");
    

    I've used new_item.line_no - but you need to put the correct column names and values in there.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题