繁体   English   中英

在foreach循环中获取多个表单输入值

[英]Get multiple form inputs values in foreach loop

我正在尝试获取调查的答复,所有问题的答案都显示在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标签,因此可能有一些额外的字段对方案很重要。

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

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

/* 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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM