php - 如何修复我的代码以使用 php、mysql 在数组中保存 3 个值来进行调查?

标签 php mysql arrays pdo survey

您好,我正在尝试使用 php 和 Mysql 进行一项调查,使用下面的代码我保存每个问题的每个答案,但是当我尝试保存 comments_per_questions 时,它没有将该字段保存到数组中,它只保存一个评论。

如何修复此功能以将每个问题的问题、答案和评论保存到数据库中?

提前致谢。

数据库结构

"Questions" (idquestion, question)

"Surveys" (idsurvey, idquestion, answers, comments_per_question, survey_number)

这部分代码保存调查表单中的问题和答案。

public function NewSurveyMulti($answer = array())
{           
    if(!empty($answer)) {
        foreach($answer as $questi => $value  ) {
            $this->MyDB->Write("INSERT INTO surveys (`idquestion`, `answers`,`comments_per_questions` )
                    VALUES( 
                    '".$questi."', 
                    '".$value[0]."',
                    '".$_POST["comment"]."')");
        }
    }

survey_form.php

<?php
    // Fetch questions
    $cuestionario   =   $con->Fetch("SELECT * FROM questions"); ?>

    <form name="newDona" action="" method="post">
    </table><?php
    // Confirm there are questions being drawn from database
    $numrows        =   (is_array($cuestionario))? count($cuestionario): 0;
    if($numrows > 0) {
            // Loop through questions
            foreach($cuestionario as $row) { ?>
            <tr>
                <!-- Write the question -->
                <td><?php echo $row["question"];?></td>
            </tr>
            <th>
                <!-- Set the question id -->
                <select name="answer[<?php echo $row['idquestion']; ?>][]">
                    <option value=""></option>
                    <option value="1">yes</option>
                    <option value="no">NO</option>
                </select>
            </th><?php 


    <th><textarea type="text" name="comment" maxlength="50" cols="130" rows="5"/ ></textarea></th>


                     } ?>


<tr>
                <td colspan="5" align="center">
                    <input type="submit" name="send" id="send" value="SAVE" />
                </td>
            </tr>
        </table>
    </form>
    <?php } ?>

最佳答案

好的,现在问题显示了循环内的注释字段,请执行您对选择所做的操作。

<textarea type="text" name="comment[<?php echo $row['idquestion']; ?>]" maxlength="50" cols="130" rows="5"/ >

并将其收集到循环中

$_POST["comment"][$questi]

旁白:你应该 prevent SQL injection因为您的代码使您的网站容易受到攻击。

关于php - 如何修复我的代码以使用 php、mysql 在数组中保存 3 个值来进行调查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26550415/

相关文章:

php - 社交媒体 PHP 动态元标签

php - Bootstrap 表使用连续数据从 MySQL 轮询更新

mysql - perl脚本同时连接两个数据库

php - 使用返回 0 的查询行比较 mysql 密码哈希(不工作)

php - 如果除了 url 之外什么都没有创建网站的屏幕截图图像?

php - Flex - 增加 PHP 服务函数调用的超时

mysql - 使用php打印mysql数据

java - 为什么 Java 中的数组大小限制为 "int"?

c - 无法将 int 数组复制到给定结构的结构地址中

java - 批处理文件 : "Environment Variable Not Defined"