php - 在 while 循环中获取表单值并插入到 MySQL 表中

标签 php mysql sql

我正在尝试将表单值插入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);
            }
        }   
    }
}

表单已提交

enter image description here

结果屏幕截图

enter image description here

最佳答案

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

"questionid[]"是用另一种方式写的,所以它是: "questionid[<?php echo $counter;?>]"等等...

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

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

相关文章:

mysql - 如何高效播种Devise用户?

sql - 如何在不关闭防火墙的情况下允许未知 IP 地址访问 Azure SQL 数据库?

PHP Laravel : Conditional update query from array

mysql - 将 MySQL 数据库连接到 Apache Superset

mysql - MySQL 存储过程默认是事务性的吗?

sql - 如何为 SQL 中的列组合提供唯一约束,而不考虑 Order

sql - Access SQL 根据条件从组中选择最新记录

php - 从 db 中获取单选按钮值并且还希望检查单选按钮

php - 我的 JOIN + GROUP BY ... HAVING COUNT 查询是否正确?

php - 将 png 合并到 jpg 之上并使用 php 保持透明度