gpt4 book ai didi

php - 从 MySQL 表中获取数据并插入到另一个表中

转载 作者:行者123 更新时间:2023-11-29 08:04:46 25 4
gpt4 key购买 nike

我在 while 循环中从 MySQL 表中获取数据,并将表单数据插入到 foreach 操作页面中的另一个 MySQL 表中,但我没有得到单选按钮的正确值,我正在附加我的代码,请帮忙。

<?php
$i = 1;
$j = 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>
";
$i++;
} else if ($row['answer_type'] == "Comments") {
echo "<textarea name='answer[]' cols='' rows=''></textarea>";
$j++;
}
echo "<br />";
?>

</div>
</div>
<?php } ?>

操作文件代码

foreach($_POST['questionid'] as $key=>$questionid){

$questionid = $_POST['questionid'][$key];
$answer = $_POST['answer'][$key];


$result3 = mysqli_query($con, "select question,answer_type from questions where question_id=$questionid;");
while($row = mysqli_fetch_array($result3)) {
$question = $row['question'];
$answer_type = $row['answer_type'];

if($answer_type == "Comments") {
$query2 = "insert into review_details (review_id,survey_id,question_id,question,answer_freeresponse) values(1,$_SESSION[surveyid],$questionid,'$question','$answer')";
$result2 = mysqli_query($con,$query2);
if(!$result2) {
echo mysqli_error($result2);
}
}
else if($answer_type == "Ratings") {
$query2 = "insert into review_details (review_id,survey_id,question_id,question,answer_rating) values(1,$_SESSION[surveyid],$questionid,'$question',$key)";
$result2 = mysqli_query($con,$query2);
if(!$result2) {
echo mysqli_error($result2);
}
}
}

$i++;
}

输出

enter image description here

我想存储显示为单选按钮的评级,我猜它会将计数器作为变量 $key 递增,我不知道如何存储单选按钮的值。

最佳答案

我想你会得到你的单选按钮值,

$ratingKey = "rating".$key;
$rating = $_POST[$ratingKey];

不要在插入查询中使用 $ rating 而不是 $key。

INSERT into review_details (review_id,survey_id,question_id,question,answer_rating) 
values(1,$_SESSION[surveyid],$questionid,'$question',$rating)

关于php - 从 MySQL 表中获取数据并插入到另一个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22954192/

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