douzong7283 2014-04-09 06:09
浏览 89
已采纳

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

I am fetching the data from the MySQL table in a while loop, and inserting the form data into another MySQL table in action page in foreach, but I do not get the correct value of the radio button, I am attaching my code, please help.

<?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 } ?>

Action File Code

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++;
}

Output

enter image description here

I want to store the ratings displayed as a radio button, I guess its taking the counter incremented as variable $key, I don't know how to store the values of the radio button.

  • 写回答

1条回答 默认 最新

  • douzhong3038 2014-04-09 06:13
    关注

    I guess you will get your radio button value as,

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

    Than use $rating in your insert query instead of $key.

    INSERT into review_details (review_id,survey_id,question_id,question,answer_rating) 
    values(1,$_SESSION[surveyid],$questionid,'$question',$rating)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决