我正在尝试获取调查的答案,所有问题的答案都显示在while循环中,并且在运行中的PHP页面中,我没有得到正确的答案,我只为单选按钮获得了正确的答案,我在附加了码。

<?php
    $i = 1;
    while ($row = mysqli_fetch_array($questions)) {
?>
<div class="control-group">
    <label class="control-label" for="focusedInput">(<?php echo $i; ?>)
    <?php
        $questionid = $row['question_id'];
        $question = $row['question'];
    ?>
    <input type="hidden" name="questionid" value="<?php echo $questionid; ?>" />
    <input type="hidden" name="question" value="<?php echo $question; ?>" />
    <?php echo $row['question']; ?></label>
    <div class="controls">
    <?php
        if ($row['answer_type'] == "Ratings") {
            echo "<p>
                Low<input type='radio' name='rating$i' value='1' id='rating_0'>
                <input type='radio' name='rating$i' value='2' id='rating_1'>
                <input type='radio' name='rating$i' value='3' id='rating_2'>
                <input type='radio' name='rating$i' value='4' id='rating_3'>
                <input type='radio' name='rating$i' value='5' id='rating_4'>High
            </p>";
        } else if ($row['answer_type'] == "Comments") {
            echo "<textarea name='answer' cols='' rows=''></textarea>";
        }
        $i++;
        echo "<br />";
    ?>
    </div>
</div>
<?php } ?>


动作文件代码:

foreach($_POST as $val){
    $query2 = "insert into review_details (review_id,survey_id,question_id,question,answer_rating,answer_freeresponse) values (1,$_SESSION[surveyid],$_POST[questionid],'$_POST[question]',$val,'$_POST[answer]')";

    $result2 = mysqli_query($con,$query2);

    if(!$result2) {
        echo mysqli_error($result2);
    }
}




我想将调查答案插入MySQL表中,其中包括输出图片中显示的字段。

最佳答案

公开的代码中缺少您的form标记,因此可能有一些对于方案很重要的额外字段。

没有明显的理由在foreach上使用$_POST。您应该进一步解释为什么要骑自行车。

这是您的操作文件的一些代码,可能对您有用:

/* create a prepared statement */
$stmt =  $mysqli->stmt_init();
if ($stmt->prepare("INSERT INTO review_details (review_id,survey_id,question_id,question,answer_rating,answer_freeresponse) VALUES(1,?,?,?,?,?)")) {

    /* bind parameters for markers */
    $stmt->bind_param("sssss", $_SESSION['surveyid'], $_POST['questionid'], $_POST['question'], $val, $_POST['answer']);

    /* execute query */
    $stmt->execute();

    /* close statement */
    $stmt->close();
}

关于php - 在foreach循环中获取多个表单输入值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22930492/

10-13 09:13