Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何读取动态生成的HTML单选按钮值并使用PHP将其插入数据库?_Php_Html_Mysql - Fatal编程技术网

如何读取动态生成的HTML单选按钮值并使用PHP将其插入数据库?

如何读取动态生成的HTML单选按钮值并使用PHP将其插入数据库?,php,html,mysql,Php,Html,Mysql,我试图创建一个动态调查,从这个意义上说,用户/管理员可以创建当调查从应用程序中得到回答时出现的问题,而不是硬编码 到目前为止,我的代码是: <div class="surveybox"> <ol> <?php $result = mysqli_query($db, "SELECT QuestionGroup FROM surveyquestions"); $unique

我试图创建一个动态调查,从这个意义上说,用户/管理员可以创建当调查从应用程序中得到回答时出现的问题,而不是硬编码

到目前为止,我的代码是:

    <div class="surveybox">
        <ol>
            <?php
            $result = mysqli_query($db, "SELECT QuestionGroup FROM surveyquestions");
            $unique = array();
            while ($row = $result->fetch_assoc()) {

                $header = $row["QuestionGroup"];
                $unique[] = $header;
            }

            foreach (array_unique($unique) as $name) {
                $questionsget = mysqli_query($db, "SELECT * FROM surveyquestions WHERE QuestionGroup = '$name'") or die(mysql_error());
                echo('<form action="GET" method="storesurveyresponse.php">');
                echo '<div class="questionheader"><h2>' . $name . '</h2></div><br>';
                while ($row = $questionsget->fetch_assoc()) {
                    $id = $row["QuestionID"];
                    $questiongroup = $row["QuestionGroup"];
                    $questiontext = $row["Question"];
                    $responsefield = $row["ResponseField"];
                    $questionID = $row["QuestionID"];




                    echo '<div class="questionbody"><li>' . $questiontext . '</li><br><br>';
                    if ($responsefield == "Radio Button") {
                        echo '<input type="radio" name="' . $questionID . '" value="Excellent">Excellent<br>
                            <input type="radio" name="radio' . $questionID . '" value="Good">Good<br>
                            <input type="radio" name="radio' . $questionID . '" value="Average">Average<br>
                            <input type="radio" name="radio' . $questionID . '" value="Good">Below Average<br>
                            <input type="radio" name="radio' . $questionID . '" value="Poor">Poor ';
                        $answer = $_GET["radio".$questionID]; //filter_input(INPUT_POST, 'radio' . $questionID);
                    } elseif ($responsefield == "Comment Box") {
                        echo '<textarea class="questionarea" name="question" placeholder="Enter Answer here..."></textarea></div>';
                    }


                    echo '<div class="responsebutton"> <button class="submit"><a class="button" href="storesurveyresponse.php?ownerID=0&questionID=' . $questionID . '&questiontext=' . $questiontext . '&questiongroup=' . $questiongroup . '&responsefield=' . $responsefield . '&answer=' . $answer . '"=>Submit</a></button></div><br><br><br><br></div>';
                echo'</form>';

                    }
            }
            ?>
        </ol>
    </div>

当从数据库中布置问题并在提交时插入所有其他必需的数据库行值(所选单选按钮值除外)时,上述代码可以完美地工作。单选按钮值列始终为空。 在所有加载的问题下,我在页面加载“注意:未定义的索引:C:\xampp\htdocs中的radio38…第52行”时也会出现以下错误,但在我提交了对其中一个问题的答复后,这些错误消失了,我不知道它们是否相关

如您所见,我使用$\u GET方法提交值,我的处理代码如下:

<?php
    include 'session.php';
    $ownerID = filter_input(INPUT_GET, ownerID);
    $questionsID = filter_input(INPUT_GET, questionID);
    $questionsText = filter_input(INPUT_GET, questiontext);
    $questionsgroup = filter_input(INPUT_GET, questiongroup);
    $replyfield = filter_input(INPUT_GET, responsefield);
    $answer = filter_input(INPUT_GET, answer);
    $date =  date("Y/m/d"); 
    mysqli_query($db, "INSERT INTO surveyanswers 
                        (OwnerID, UserID,ParticipantName, DateStart, 
                        DateEnd, QuestionID, QuestionText, QuestionGroup,   
                        ResponseField, Answer) 
                VALUES ('$ownerID', 'aaa', 'aaa', '$date', 
                        '$date','$questionsID', '$questionsText', '$questionsgroup', 
                        '$replyfield','$answer')")or die(mysqli_error($db)); 
    header("Location: takesurvey.php");

我做了一些修改,最后根据您的建议和建议使它开始工作。我将get方法添加到表单声明中,并将处理代码放在与表单相同的页面上

代码如下:

    foreach (array_unique($unique) as $name) {
                $questionsget = mysqli_query($db, "SELECT * FROM surveyquestions WHERE QuestionGroup = '$name'") or die(mysql_error());
                echo '<div class="questionheader"><h2>' . $name . '</h2></div><br>';
                while ($row = $questionsget->fetch_assoc()) {
                    $id = $row["QuestionID"];
                    $questiongroup = $row["QuestionGroup"];
                    $questiontext = $row["Question"];
                    $responsefield = $row["ResponseField"];
                    $questionID = $row["QuestionID"];

                    echo '<form action="" method="get">';

                    echo '<div class="questionbody"><li>' . $questiontext . '</li><br><br>';
                    if ($responsefield == "Radio Button") {
                        echo '<input type="radio" name="radio'. $questionID . '" value="Excellent">Excellent<br>
                            <input type="radio" name="radio' . $questionID . '" value="Good">Good<br>
                            <input type="radio" name="radio' . $questionID . '" value="Average">Average<br>
                            <input type="radio" name="radio' . $questionID . '" value="Good">Below Average<br>
                            <input type="radio" name="radio' . $questionID . '" value="Poor">Poor ';
                        $answer = "radio" . $questionID; //filter_input(INPUT_POST, 'radio' . $questionID);

                        echo '<input type="submit" name="submit" value="Submit">';

                        if (isset($_GET['submit'])) {
                            if (isset($_GET['radio' . $questionID])) {
                                $answer = filter_input(INPUT_GET, 'radio' . $questionID);

                                $ownerID = "aaa";
                                $questionsID = $questionID;
                                $questionsText = $questiontext;
                                $questionsgroup = $questiongroup;
                                $replyfield = $responsefield;

                                $date = date("Y/m/d");
                                mysqli_query($db, "INSERT INTO surveyanswers(OwnerID, UserID, ParticipantName, DateStart, DateEnd, QuestionID, QuestionText, QuestionGroup, ResponseField, Answer) VALUES ('$ownerID', 'aaa', 'aaa', '$date', '$date', '$questionsID', '$questionsText', '$questionsgroup', '$replyfield', '$answer')")or die(mysqli_error($db));
                            }
                        }
                    } elseif ($responsefield == "Comment Box") {
                        echo '<textarea class="questionarea" name="question" placeholder="Enter Answer here..."></textarea></div>';
                    }
                    echo '</form>';
                }
            }
foreach(数组_unique($unique)作为$name){
$questionsget=mysqli_query($db,“从surveyquestions中选择*,其中QuestionGroup='$name'))或die(mysql_error());
回显“.$name.”
; 而($row=$questionsget->fetch_assoc()){ $id=$row[“问题id”]; $questiongroup=$row[“questiongroup”]; $questiontext=$row[“问题”]; $responsefield=$row[“responsefield”]; $questionID=$row[“questionID”]; 回声'; 回显“
  • ”.$questiontext.

  • ”; 如果($responsefield==“单选按钮”){ echo“优秀”
    好的
    平均值
    低于平均水平
    可怜的; $answer=“radio”。$questionID;//过滤输入(输入\u POST,'radio'.$questionID); 回声'; 如果(isset($_GET['submit'])){ 如果(isset($\u GET['radio.$questionID])){ $answer=filter\u input(input\u GET,'radio'.$questionID); $ownerID=“aaa”; $questionsID=$questionID; $questionsText=$questiontext; $questionsgroup=$questiongroup; $replyfield=$responsefield; $date=日期(“Y/m/d”); mysqli_query($db),“插入到调查问卷中回答(所有者ID、用户ID、参与者姓名、日期开始、日期结束、问题ID、问题文本、问题组、响应字段、答案)值(“$OwnerID”、“aaa”、“aaa”、“aaa”、“$date”、“$date”、“$questionsID”、“$questionsText”、“$questionsgroup”、“$replyfield”、“$Answer”)或die(mysqli_错误($db)); } } }elseif($responsefield==“注释框”){ 回声'; } 回声'; } }

    谢谢

    您甚至没有尝试拾取和存储单选按钮值。从这里开始为什么你有一个
    和一个锚定标签?谢谢@Ryan Vincent。。$answer变量已插入数据库,我知道为什么RiggsFolly说我不想获取单选按钮的值。@RiggsFolly,在这种情况下,我如何获取所选单选按钮的值?当我认为问题可能是因为没有以正确的格式提交问题时,添加了标记。首先阅读错误日志,然后将第二个参数修复为所有
    filter\u input()
    函数,以使第二个参数有效,例如
    filter\u input(input\u GET,'ownerID')