php - 需要帮助将调查响应从 HTML 表单传递到 MySQL 表

标签 php html mysql forms http-post

我在这里有点难过......

我有一个网页显示了存储在数据库表中的调查问题列表,我正在尝试收集每个问题的评分答复(强烈反对 -> 强烈同意)并将值传回到我的数据库中的另一个表。

这是表单代码的片段:

<form action="submitAnswers.php" name="subAns" method="post">
        <fieldset>
            <legend>Survey Questions</legend>
            <input type="hidden" name="survey_id" value="<?php echo ($survey_id); ?>">
            <table width=90% border=0 cellpadding='2' cellspacing='2'?>

            <tr bgcolor=#D0D0D0>

                <th>Question</th>
                <th>Strongly Disagree</th>
                <th>Somewhat Disagree</th>
                <th>Neither Disagree nor Agree</th>
                <th>Somewhat Agree</th>
                <th>Strongly Agree</th>
            </tr>
            <tr>    
            <?php
                while ($row  =  mysql_fetch_array($res, MYSQL_ASSOC)) 
                {
                    echo "<td bgcolor='#E8E8E8'><font size='-1'>$row[quest_order]) $row[quest_text]</font></td>";

                    for ($i = 0; $i < 5; $i++) 
                    {
                    //echo "<input type='hidden' name='qID' value='$quest_id'>";

                    echo "<td bgcolor='#E8E8E8'><input type='radio' name='$row[quest_id]' value='$i'></td>";
                    }           

                    echo "</tr>";
                }
            ?>      

            <br>            
            </table><br>
            <input type="submit" name="submitAnswers" value="Submit Survey"></fieldset>
    </form>

这是“submitAnswers.php”文件中的 PHP:

$survey_id=$_POST['survey_id'];

    $sql = "INSERT INTO survey_responses (survey_id) 
            VALUES ('$survey_id')";

    $res = send_sql($sql, $link, $db);  


    foreach($_POST['quest_id'] as $id=>$value)
    {   

        $sql = "INSERT INTO survey_answers (response_id, quest_id, response_value) 
            VALUES (LAST_INSERT_ID(), $id, '$value')";

        $res = send_sql($sql, $link, $db) or die("Cannot add survey");

    }

我在“survey_responses”表中的插入语句工作正常,但我不知道如何更正用于获取每个问题的响应分数的单选按钮。这些值是通过 POST 传递的(我通过查看 print_r($_POST); 确认了这一点,它返回了,例如:

Array ( [survey_id] => 4 [6] => 0 [5] => 1 [4] => 2 [3] => 3 [2] => 4 [1] => 3 [submitAnswers] => Submit Survey )

但显然我的做法是错误的,因为我无法成功迭代并为每个问题插入问题 ID# 和响应值。我敢肯定这相对简单,但我对编程还是个新手,已经研究了好几个小时,但运气不佳。

只是希望有人能帮我解决这个问题,并希望在这个过程中更好地解释,或者给我指出一个很好的资源,涵盖如何处理动态大小的表单,这些表单正在检索数据库记录并将各种信息量传递回数据库。

谢谢!

最佳答案

使用

$sql = "INSERT INTO survey_answers (response_id, quest_id, response_value) 
        VALUES ('".LAST_INSERT_ID()."', '".$id."', '."$value."')";

代替

$sql = "INSERT INTO survey_answers (response_id, quest_id, response_value) 
        VALUES (LAST_INSERT_ID(), $id, '$value')";

关于php - 需要帮助将调查响应从 HTML 表单传递到 MySQL 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13864458/

相关文章:

php - 将 7 天添加到 PHP 日期并立即更改格式

jQuery 日期选择器设置为空

php - 如何使用连接列导出到 csv

mysql - 为什么从 Django pre_save 信号触发的 celery 任务永远看不到对象

php - SQL_ASCII 数据库不显示特殊字符

php - 如何创建布局模板 - 静态网站

javascript - 使用周数和禁用日期计算结束日期

javascript - 弹跳子菜单jquery

html - 为什么 div 的内容会受到其显示的影响?

mysql - TIMEDIFF 的格式结果以天为单位 :hours:min:sec