简体   繁体   English

将表结构化的表单数据存储到数据库中-正确的方法?

[英]Storing table structured form data into database - The proper way to do it?

This is going to be a long read, so please bear with me. 这将是一本长篇小说,所以请多多包涵。 I'm pretty good at PHP, but my knowledge in database designing is poor. 我非常擅长PHP,但是我在数据库设计方面的知识很差。 I'm working on it as I've realized that having command over designing a database is the most important thing while working with applications and how things get so easier if you get the hang of it. 我正在研究它,因为我已经意识到在设计应用程序时,掌握设计数据库的命令是最重要的事情,如果掌握了它,事情将变得如此容易。 The inception of this thread is also because of my weak knowledge about database interaction. 之所以创建该线程,还因为我对数据库交互的了解不足。

I basically have around 40 forms whose structure is similar to the image given below: 我基本上有大约40种形式,其结构类似于下面给出的图像:

表格结构

The number of columns and rows vary from one form to another, but they more or less follow a similar structure as shown above. 列和行的数量从一种形式到另一种形式都不同,但是它们或多或少遵循如上所述的相似结构。

WHAT I'M PLANNING TO ACHIEVE 我打算实现的目标

  • Use a single / same php page to process (ie, insert data into database) all the 40 forms. 使用单个/相同的php页面来处理所有40种形式(即,将数据插入数据库)。 In other words, I intend to use the same <form action="process.php"> for all the forms. 换句话说,我打算对所有表单使用相同的<form action="process.php">
  • Avoid hard-coding; 避免硬编码; keep the code in process.php as much dynamic as possible. 保持process.php的代码尽可能动态。

ROADBLOCKS 路障

  • The number of parameters in each form vary, so I need to figure out a way to find out the number of rows present in each form. 每种形式的参数数量各不相同,因此我需要找出一种方法来找出每种形式中存在的行数。
  • Finding it difficult to decide how to name the elements (radio buttons, checkboxes, dropdowns, textareas, textboxes etc) being used in the form. 发现很难决定如何命名表单中使用的元素(单选按钮,复选框,下拉列表,文本区域,文本框等)。
  • I'm having trouble trying to figure out how to proceed with the insertion. 我在尝试弄清楚如何进行插入时遇到麻烦。 Inserting one row at a time seems to be appropriate, but how would I achieve this? 一次插入一行似乎很合适,但是我将如何实现呢? For example, if I use a loop, in the first iteration data related to Ambiance should be inserted. 例如,如果使用循环,则在第一次迭代中应插入与Ambiance相关的数据。 In the second loop data related to TV Room should be inserted and so on. 在第二个循环中,应插入与TV Room有关的数据,依此类推。 The problem is how to code accordingly? 问题是如何相应地编码?

WHAT I'VE IN MIND 我的想法

  • The columns of the form (parameter, meets requirement, observation, status, remarks) become the fields of the MySql table. 形式的列(参数,符合要求,观察,状态,备注)成为MySql表的字段。

  • Then insert one row at a time in the table using a loop. 然后使用循环在表中一次插入一行。

  • Use arrays for naming the elements used in the form. 使用数组来命名表单中使用的元素。 For example, ambiance[requirement], ambiance[observation], ambiance[status], ambiance[remarks] for elements in the first row and room[requirement], room[observation], room[status], room[remarks] for the second row and so on. 例如,第一行的元素的ambiance [要求],ambiance [观察],ambiance [状态],ambiance [备注]和该房间的room [要求],room [observation],room [status],room [remark]第二排,依此类推。 Then insertion can be done in a single line by using INSERT INTO tablename (implode(',', array_keys($ambiance))) VALUES (implode(',', $ambiance)) 然后可以通过使用INSERT INTO tablename (implode(',', array_keys($ambiance))) VALUES (implode(',', $ambiance))在一行中完成插入

WHAT THIS IS NOT ABOUT 这不是什么

  • Asking to supply / post code. 要求提供/邮政编码。

  • Give me teh codez is not my way of operating. Give me teh codez不是我的操作方式。 I'm just seeking instructions on how to go about with the task. 我只是在寻找有关如何完成任务的说明。

WHAT THIS IS ABOUT 这是关于什么

  • Asking for suggestions. 寻求建议。

  • Determining if I'm going in the right direction. 确定我是否朝着正确的方向前进。

  • Asking if there are alternate ways. 询问是否有其他方法。

I have done something similar but not exactly the same. 我做了类似但不完全一样的事情。 I used these two tables: 我使用了以下两个表:

  • Form 形成
    • ID ID
    • Name (eg feedback, comments, survey) 名称(例如反馈,评论,调查)

  • FormFields** FormFields **
    • ID ID
    • FormID 表格ID
    • Caption (eg name, company, address) 标题(例如姓名,公司,地址)
    • SubCaption (eg enter your full name) 子标题(例如,输入您的全名)
    • Required 需要
    • DataType (eg integer, number, string, email) 数据类型(例如,整数,数字,字符串,电子邮件)
    • MaxLength 最长长度

** Look at the schema of a database table for more ideas. **查看数据库表的架构以获取更多建议。

In your case you need another pair of tables: 在您的情况下,您需要另一对表:

  • FormSubmission 表格提交
    • ID ID
    • Date 日期

  • FormSubmissionValues 表单提交值
    • ID ID
    • FormSubmissionID 表单提交ID
    • FormFieldID (can be used to deduce FormID or you can add FormID in FormSubmission table) FormFieldID(可用于推断FormID,也可以在FormSubmission表中添加FormID)
    • Value (a field large enough to accommodate any value) 值(一个足以容纳任何值的字段)

There were several catches. 有几个收获。 I'll note down a few: 我记下一些:

  • The UI needs to vary depending on datatype (small 10 character input for integer/number, checkbox for boolean, textarea for text) 用户界面需要根据数据类型而有所不同(整数/数字为10个小字符输入,布尔值为复选框,文本为textarea)
  • Not sure how to handle list boxes. 不确定如何处理列表框。 Perhaps you need another table for this 也许您需要另一张桌子
  • You cannot use indexes properly since everything is stored in a generic text field 您不能正确使用索引,因为所有内容都存储在通用文本字段中
  • Client side validation is possible but difficult 客户端验证是可能的,但是很困难
  • The tables need to be very generic. 这些表必须非常通用。 Based on my experience this requires extra effort to ensure that x works with all y . 根据我的经验,这需要额外的精力来确保x与所有y一起使用

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM