简体   繁体   中英

How can I fix my code to save 3 values in array using php, mysql to make a survey?

Hello I am trying to make a survey using php and Mysql, with the code below I save every answer per questions, but When I tried to save comments_per_questions it did not save that field into the array it only save one comment.

How can I fix this function to save into the database Questions, Answers and Comments per questions?

Thanks in advance.

Database Structure

"Questions" (idquestion, question)

"Surveys" (idsurvey, idquestion, answers, comments_per_question, survey_number)

This part of code save the question and answers from the survey form.

public function NewSurveyMulti($answer = array())
{           
    if(!empty($answer)) {
        foreach($answer as $questi => $value  ) {
            $this->MyDB->Write("INSERT INTO surveys (`idquestion`, `answers`,`comments_per_questions` )
                    VALUES( 
                    '".$questi."', 
                    '".$value[0]."',
                    '".$_POST["comment"]."')");
        }
    }

survey_form.php

<?php
    // Fetch questions
    $cuestionario   =   $con->Fetch("SELECT * FROM questions"); ?>

    <form name="newDona" action="" method="post">
    </table><?php
    // Confirm there are questions being drawn from database
    $numrows        =   (is_array($cuestionario))? count($cuestionario): 0;
    if($numrows > 0) {
            // Loop through questions
            foreach($cuestionario as $row) { ?>
            <tr>
                <!-- Write the question -->
                <td><?php echo $row["question"];?></td>
            </tr>
            <th>
                <!-- Set the question id -->
                <select name="answer[<?php echo $row['idquestion']; ?>][]">
                    <option value=""></option>
                    <option value="1">yes</option>
                    <option value="no">NO</option>
                </select>
            </th><?php 


    <th><textarea type="text" name="comment" maxlength="50" cols="130" rows="5"/ ></textarea></th>


                     } ?>


<tr>
                <td colspan="5" align="center">
                    <input type="submit" name="send" id="send" value="SAVE" />
                </td>
            </tr>
        </table>
    </form>
    <?php } ?>

Ok, now the question shows the comment field within the loop, do what you did with the select.

<textarea type="text" name="comment[<?php echo $row['idquestion']; ?>]" maxlength="50" cols="130" rows="5"/ >

and collect it in your loop with

$_POST["comment"][$questi]

ASIDE: You should prevent SQL injection as your code leaves your site vulnerable to attack.

Here's a working example :

<?php

echo '<form method="post">';
echo '<textarea name="comments[]" rows="8" cols="40"></textarea>';
echo '<textarea name="comments[]" rows="8" cols="40"></textarea>';
echo '<textarea name="comments[]" rows="8" cols="40"></textarea>';
echo '<button name="submit">Submit</button>';
echo '</form>';

if (isset($_POST['comments']))
{
    var_dump($_POST['comments']);
}

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