在1个insert语句中插入多个记录

时间:2015-06-22 10:45:19

标签: php html sql mysqli

我有一张表格。人们填写答案的地方。

<form method="POST">
        <input type="hidden" value="true" id="x" name="x">
            <table>
                <b>Do you use a Wheel impact and Load Detection System (ATLAS FO, former GOTCHA)?</b>
                <hr />
                <input type="hidden" value="Do you use a Wheel impact and Load Detection System (ATLAS FO, former GOTCHA)?" name="question1">
                <tr>
                    <td><input type="radio" name="answer1" value="yes">Yes</td>
                </tr>
                <tr>
                    <td><input type="radio" name="answer1" value="no">No</td>
                </tr>
            </table>
                <br /><br />
            <table>
                <b>Product Reliabillity:</b>
                <hr />
                <tr>
                    <input type="hidden" value="Product Reliabillity" name="question2">
                    <td><input type="radio" name="answer2" value="Very satisfied">Very satisfied</td>
                    <td><input type="radio" name="answer2" value="Satisfied">Satisfied</td>
                    <td><input type="radio" name="answer2" value="Less satified">Less satified</td>
                    <td><input type="radio" name="answer2" value="Very unsatisfied">Very unsatisfied</td>
                    <td><input type="radio" name="answer2" value="N/A">N/A</td>
                </tr>
            </table>
                <br /><br />
            <table>
                <b>Product Handling:</b>
                <hr />
                <tr>
                    <input type="hidden" value="Product Handling" name="question3">
                    <td><input type="radio" name="answer3" value="Very satisfied">Very satisfied</td>
                    <td><input type="radio" name="answer3" value="Satisfied">Satisfied</td>
                    <td><input type="radio" name="answer3" value="Less satified">Less satified</td>
                    <td><input type="radio" name="answer3" value="Very unsatisfied">Very unsatisfied</td>
                    <td><input type="radio" name="answer3" value="N/A">N/A</td>
                </tr>
            </table>
            <input type="submit" name="Add" value="submit">
        <?php 
            if(isset($_POST['x'])){
            $validcomment = false;
            //validate check This should work but have not build in all the checks yet!!!!
            if(!empty($_POST['answer2'])){
                $validcomment = true;
            }else{
                echo "Please fill in all the questions!" . "<br>";
            }


        //If everything is okay, do this.
        if($validcomment){
                unset($_POST['x']);  
                    echo $_POST['answer1'] . "<br />";
                    echo $_POST['answer2'] . "<br />";
                    echo $_POST['answer3'] . "<br />";
                    echo $_POST['answer4'] . "<br />";
                    echo $_POST['answer5'] . "<br />";
                    echo $_POST['answer6'] . "<br />" . "<br />";

                    echo $_POST['question1'] . "<br />";
                    echo $_POST['question2'] . "<br />";
                    echo $_POST['question3'] . "<br />";
                    echo $_POST['question4'] . "<br />";
                    echo $_POST['question5'] . "<br />";
                    echo $_POST['question6'] . "<br />";
                }
            }
        ?>
</form>

现在,当我插入它们时,它会向我显示结果。 但是,我想将它们插入我的数据库中。 我的数据库如下所示:

My database (评论将在以后使用,因此现在不重要。)

我当前的插入语句如下所示:

function enquete_insert() {
        $sql = "INSERT INTO enquete "
    . "(enquete_id, question, answer)"
    . "VALUES (enquete_id, :question, :answer) ";
        $sth = $this->pdo->prepare($sql);
        $sth->bindParam(':question', $_POST['question'], PDO::PARAM_STR);
        $sth->bindParam(':answer', $_POST['answer'], PDO::PARAM_STR);
        $sth->execute();
}

当我为问题1命名时,此功能正常工作:question和1个答案answer。此外,它只会插入1个答案/问题。

但是我需要在我的数据库中添加(在这个例子中)3条记录,并提出问题并回答。

0 个答案:

没有答案