[英]Insert HTML form data to mySQL via PHP (form array vary from amount of table rows)
I have a HTML form where users can checkmark up to 50 different questions. 我有一个HTML表单,用户可以在其中选中50个不同的问题。 They have to checkmark minimum 1 and at the most all 50 questions.
他们必须打勾最少1个问题,最多50个问题。
After they submit the form I need to insert the data ( form_array
) to my mySQL table questions . 他们提交表单后,我需要将数据(
form_array
)插入到mySQL表问题中 。 The table contains of 50 questions rows (see below question table ex.). 该表包含50个问题行(请参阅下面的问题表,例如)。
I know the INSERT INTO 'questions' (question1, question2, question3, question4,....question50) VALUES (value1, value2, value3...)
, but my challenge is that since the amount of checked questions (values) can vary in the form, I do not know how to insert the form_array
to my questions table. 我知道
INSERT INTO 'questions' (question1, question2, question3, question4,....question50) VALUES (value1, value2, value3...)
,但是我面临的挑战是,由于检查的问题(值)的数量可以形式不同,我不知道如何将form_array
插入我的问题表。
The questions are inserted as true or false in the form_array
depending on if they are marked or unmarked in the html form. 根据是在html表单中标记问题还是未标记问题 ,将问题以true或false插入到
form_array
。
The questions table has a primary auto incremented ID and 2 foreign keys besides the 50 questions. 除了50个问题外, 问题表还有一个主要的自动递增ID和2个外键。
I welcome all suggestions/examples on how to insert an array with the above scenario? 我欢迎有关如何在上述情况下插入数组的所有建议/示例?
The question table will look like this: 问题表将如下所示:
`question1` tinyint(1) NOT NULL,
`question2` tinyint(1) NOT NULL,
`question3` tinyint(1) NOT NULL,
`question4` tinyint(1) NOT NULL,
`question5` tinyint(1) NOT NULL,
`question6...etc etc...up to 50 questions
I think you want to store the answers to the 50 questions, right? 我想您想存储50个问题的答案 ,对不对?
First of all, your table structure should look something like this: 首先,您的表结构应如下所示:
questions
---------
id: primary key, int(11), auto increments
question/title: varchar(255)
answers
-------
id: primary key, int(11), auto increments
question_id: int(11)
answer: tinyint(1)
This is the tricky part. 这是棘手的部分。 To display the questions (I used mysqli as I don't know what you're using):
显示问题(我使用mysqli,因为我不知道您在使用什么):
$result = $mysqli->query("SELECT * from questions");
while ($question = $result->fetch_assoc()) {
print 'Question: ' . $question['title'];
print '<input type="hidden" name="answers[]['question_id']" value="' . $question['id'] . '">';
print '<input type="radio" name="answers[]['answer']" value="1"> Yes';
print '<input type="radio" name="answers[]['answer']" value="0"> No';
}
Now when a user submits his answers: 现在,当用户提交答案时:
if(isset($_POST['submit'])) {
$answers = $_POST['answers'];
if(count($answers) > 1) {
//Loop through all the answers
foreach($answers as $answer) {
$mysqli->query("INSERTO INTO answer ('question_id', 'answer') VALUES ('{$answer['question_id']}', '{$answer['answer']')"); //Insert the answers one by one
}
} else {
print 'You need to submit at least one answer!'
}
}
I haven't tested the code. 我还没有测试代码。 This is still pretty basic, but it's exactly what you need, I think... :-).
这仍然是非常基本的,但是我认为这正是您所需要的... :-)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.