简体   繁体   中英

Get multiple form inputs values in foreach loop

I am trying to get the response of a survey, all question answers are displayed in a while loop and in action PHP page I am not getting proper responses, I'm getting the right response for the radio button only, I'm attaching the code.

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

Action file code:

foreach($_POST as $val){
    $query2 = "insert into review_details (review_id,survey_id,question_id,question,answer_rating,answer_freeresponse) values (1,$_SESSION[surveyid],$_POST[questionid],'$_POST[question]',$val,'$_POST[answer]')";

    $result2 = mysqli_query($con,$query2);                                                      

    if(!$result2) {
        echo mysqli_error($result2);
    }
}

在此处输入图片说明

I want to insert the survey answers in the MySQL table including the fields displayed in the output picture.

Your form tags are missing from the exposed code, so there might be some extra fields available that are important for the scenario.

There is no obvious reason for using foreach on $_POST . You should explain further why you're cycling it.

Here is some code for your action file, that might work for you:

/* create a prepared statement */
$stmt =  $mysqli->stmt_init();
if ($stmt->prepare("INSERT INTO review_details (review_id,survey_id,question_id,question,answer_rating,answer_freeresponse) VALUES(1,?,?,?,?,?)")) {

    /* bind parameters for markers */
    $stmt->bind_param("sssss", $_SESSION['surveyid'], $_POST['questionid'], $_POST['question'], $val, $_POST['answer']);

    /* execute query */
    $stmt->execute();

    /* close statement */
    $stmt->close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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