gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:25:24 27 4
gpt4 key购买 nike

我正在尝试获取调查的响应,所有问题答案都显示在 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);
}
}

enter image description here

我想将调查答案插入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();
}

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com