doufu6130 2013-06-12 14:42
浏览 41

PHP / MySQL语法插入多个表(多对多结构)

I am trying to insert data from one form into multiple MySQL tables (using MYSQL syntax, although I will be migrating the application MYSQLi at some point - this probably isn't relevant right now, but thought I'd say it to stop the "you should be doing this in MYSQLi" comments that inevitably follow - I know I should, and I will be... soon... but for a number of reasons out of my control, I can't yet, and we still need this working).

Essentially, I am inserting into a "many-to-many" table structure (i.e. instances that can be part of many categories, and categories that can include many instances). The main table Instances, has all of the main pieces of data (in this case, places on a map - for example 'Acme Cafe', 'Lakeside Picnic Area', etc); another table Categories includes all of the possible categories for all of the places in the instances table - for example, 'Cafe', 'Pub', 'View Point', etc; and a joining table Instance_Categories, connects the places in the Instances table to the categories in the Categories table.

The PHP form has the following fields:

  • Reference (a 9 digit randomly generated identifier used to identify the instance with it's many categories in the Instance_Categories table)
  • Name (text box)
  • Address (text box)
  • Phone (text box)
  • Tick-box fields for each of the categories (8 of them).

The Instances table has the following columns

  • ID (unique for the table)
  • Reference (see form input above)
  • Name
  • Address
  • Phone

The Categories table has the following columns

  • ID (unique to the table)
  • Category (varchar)
  • Category_Ref (a number to identify the reference in the connecting Instances_Categories table)

The Instances_Categories table has the following columns:

  • ID (unique to the table)
  • Instance_Ref (from the form input above)
  • Category_Ref (relating to the Categories table above)

How do I formulate the INSERT statement in order to insert the data into my database? Can I just formulate an insert command for each table and run them one after the other from the same "Submit" button? And how would I get the insert command to dynamically insert more rows into the Instances_Categories table depending on how many category tick-boxes have been selected? I'm tearing my hair out!

  • 写回答

1条回答 默认 最新

  • dourui7186 2013-06-12 14:50
    关注

    If you're wanting to insert to multiple tables with one query you can't.

    You could use a transaction, but just run the multiple queries and call it a day.

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行