【问题标题】:fetch form values in while loop and insert into MySQL table在while循环中获取表单值并插入MySQL表
【发布时间】:2014-04-09 12:18:21
【问题描述】:

我正在尝试将表单值插入 MySQL 表中,但表单处于 while 循环中,所以基本上我想添加多个值,我正在使用 foreach 但由于某些问题,只有前两个值被插入到表中其余的值要么缺失要么不正确,我附上了代码和结果截图。

<?php
$i = 1;
$counter = 1;
while ($row = mysqli_fetch_array($questions)) {
    ?>
    <div class="control-group">

        <label class="control-label" for="focusedInput">(<?php echo $counter; ?>)
            <?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>";
            }
            echo "<br />";
            $counter++;
            ?>

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

动作文件代码

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

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

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

    $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',$rating)";         
        $result2 = mysqli_query($con,$query2);                                                          
        if(!$result2) {
        echo mysqli_error($result2);
            }
        }   
    }
}

表单提交

结果截图

【问题讨论】:

    标签: php mysql sql


    【解决方案1】:

    您应该在代码中名称的所有括号中添加$counter,例如:"answer[]" 变为:"answer['.$counter.']"

    "questionid[]" 是另一种写法,所以它变成:"questionid[&lt;?php echo $counter;?&gt;]",等等...

    这将解决您当前与迭代数组有关的问题,但我建议您检查并重写您的所有代码。您所有的 sql 查询都对注入开放......

    【讨论】:

    • @RajeshVishnani - 刚刚发现它可能会发生并更新了答案。
    • 嗨,不应该是我的文本区域的名称 'answer$counter' 而不是 "answer['.$counter.']",因为除了这个之外,我得到的所有答案都是正确的。
    • 'answer$counter' 也是一个选项。您应该将服务器端代码更改为从 $_POST['answer'.$key] 读取。
    • 否 :( 我试过了,没有得到答案类型为 cmets 的答案的正确值。
    • 如果它适用于$rating,它也应该适用于$answer。仔细检查您的新代码是否有拼写错误。
    猜你喜欢
    • 2021-06-03
    • 1970-01-01
    • 2018-02-18
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 2013-12-16
    相关资源
    最近更新 更多