0

i am trying to insert form values into MySQL table, but the form is in while loop, so basically i want to add multiple values, i am using foreach but due to some problem only first two values are being inserted into the table and rest of the values are either missing or in correct, i am attaching the code as well as the result screenshot.

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

Action File Code

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

Form Submitted

enter image description here

Result Screen Shot

enter image description here

1 Answer 1

1

You should add $counter in all the brackets of the names in your code like: "answer[]" becomes: "answer['.$counter.']".

"questionid[]" is written in another way, so it beocmes: "questionid[<?php echo $counter;?>]", etc...

That would solve your current issues with iterating arrays, but I would advice to inspect and rewrite all of your code. All of your sql queries are wide open to injection...

Sign up to request clarification or add additional context in comments.

7 Comments

@RajeshVishnani - just spotted it might happen and updated the answer.
hi, shouldn't be my text area's name 'answer$counter' instead "answer['.$counter.']", because i get correct answers for all except this.
'answer$counter' is an option, too. You should change the server side code to read from $_POST['answer'.$key].
no :( i tried that, do not get the correct value for the answers whose answer type is comments.
If it works for $rating it should work for $answer, too. Double check your fresh code for typos.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.