简体   繁体   English

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

[英]Fetching data from a MySQL table and inserting into another table

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. 我在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 } ?>

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 产量

在此输入图像描述

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. 我想将显示的评级存储为单选按钮,我猜它将计数器增加为变量$ key,我不知道如何存储单选按钮的值。

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. 在插入查询中使用$rating而不是$ key。

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

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

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