PHP表单进入mySql db。(PHP form into mySql db. Array formatting question)

编程入门 行业动态 更新时间:2024-10-28 02:27:41
PHP表单进入mySql db。(PHP form into mySql db. Array formatting question)

我正在处理一个允许用户复制表单输入组的表单(请参阅此处: http : //jsfiddle.net/Sd9Ag/5/ )。

我想将数据插入到具有两个表的数据库中:“question”(包含列:“QID”,“text”)和“answer”(包含列:“AID”​​,“QID”,“text”)。

当我提交表单时,信息会像这样发布(在此示例中,表单有2个问题,都有3个答案):


题 :

Array ( [0] => question 1 [1] => question 2 )

回答:

Array ( [0] => q1 answer 1 [1] => q1 answer 2 [2] => q1 answer 3 [3] => q2 answer 1 [4] => q2 answer 2 [5] => q2 answer 3 )

我的问题是:在循环遍历post变量以将它们插入数据库时​​,将这些问题与其答案相关联的最佳方法是什么?

我应该编写一个方法,使每个表单输入组的name不同吗?

有没有办法将这些输入“集合”中的每一个分组到一个数组中?

I am working on a form that allows the user to duplicate groups of form inputs (see here: http://jsfiddle.net/Sd9Ag/5/ ).

I would like to insert the data into a database with two tables: "question" (containing columns: "QID", "text") and "answer" (containing columns: "AID", "QID", "text").

When I submit the form, the information is posted like this (in this example the form has 2 questions, both having 3 answers):


question :

Array ( [0] => question 1 [1] => question 2 )

answer :

Array ( [0] => q1 answer 1 [1] => q1 answer 2 [2] => q1 answer 3 [3] => q2 answer 1 [4] => q2 answer 2 [5] => q2 answer 3 )

My question is: what is the best way to keep these questions linked to their answers when looping through the post variables to insert them into the database?

Should I script a way for the name of each form input group to be different?

Is there a way to group each of these input "sets" into an array?

最满意答案

我在过去做过类似的事情,其中​​包含一个调度小部件,允许您为日程安排添加日期,就像您有问题的答案一样。 我处理它的方式是通过javascript添加额外的行,增加计数器来命名字段。

像这样的东西:

<input type="text" name="question[1]"> <input type="text" name="answer[1][1]">

现在你在javascript中保留一个计数器,你有多少答案,所以当你添加一个新答案时,你只需增加该计数器。 例如,如果您以默认的1个答案开头:

var count = 1 //creating new answer field logic count++; answer_name = 'answer[' + question + '][' + count + ']';

当然,它有点复杂,但希望你能够了解我的目标。

然后当你提交时,你抓住你的问题数组然后只是查询答案数组以获得该问题的所有答案。 问题1的答案将在$ answers [1]中作为数组。

I've done something similar in the past with a scheduling widget that allowed you to add days to a schedule, like you have answers to a question. The way I handled it was to add in the extra lines via javascript, incrementing counters to name the fields.

Something like this:

<input type="text" name="question[1]"> <input type="text" name="answer[1][1]">

Now you keep a counter in javascript of how many answers you have for the question, so when you add a new answer you just increment that counter. For instance, if you start with a default of 1 answer:

var count = 1 //creating new answer field logic count++; answer_name = 'answer[' + question + '][' + count + ']';

Of course, it's a little more complicated than that, but hopefully you get the idea of where I'm going with this.

Then when you submit, you grab your question array and then just query the answer array to get all of the answers for that question. Question 1's answers will be in $answers[1] as an array.

更多推荐

本文发布于:2023-07-22 12:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219891.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   db   mySql   PHP   formatting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!