简体   繁体   English

将来自多个文本字段的数据插入MYSQL数据库

[英]Insert data from multiple textfields into MYSQL Database

Basically, I am able to display the DIVs based on the selection of drop box. 基本上,我可以基于下拉框的选择显示DIV。 now my problem is : I am able to insert data when there is only single text field. 现在我的问题是:只有一个文本字段时,我才能插入数据。 When there are 2 text fields, the submission is a success, but the data cannot be found in the database. 当有2个文本字段时,提交成功,但是在数据库中找不到数据。 1 row in the database is taken though. 但是,数据库中已占用1行。

<?php
//database
$connection = mysql_connect("localhost", "root", "") or die (mysql_error());
mysql_select_db("survey" , $connection) or die (mysql_error());

if(isset($_POST['submit'])){

$surveyID =$_POST['surveyCategory'];

for($i=0; $i<count($_POST['id']); $i++){
        $questionID = $_POST['id'][$i];
        $answer = mysql_real_escape_string(htmlspecialchars($_POST['answer'][$i]));
        mysql_query("INSERT INTO answers(survey_id, question_id, answer_body) VALUES   ('$surveyID', '$questionID', '$answer')") or die (mysql_error());
}
}



?>

<style>
div{
    display: none;
}
</style>


<html>
<body>
<form name=displayQuestion method="post">
    Survey Categories : 
    <select name="surveyCategory" id="surveyCategory">
    <option> Choose Survey Category </option>
    <?php
        $surveyQuery = "SELECT survey_id, survey_name FROM surveys";
        $result = mysql_query($surveyQuery) or die (mysql_error());
        while($menu=mysql_fetch_assoc($result)){
        echo "<option value=$menu[survey_id]>$menu[survey_name]</option>";              
        }
    ?>
    </select>

<!-- if selection is auction -->
<div id="1" style="display:none">
    <?php
    $auctionSurvey = "SELECT question_id, survey_id, question_body FROM questions 
                      WHERE survey_id='1'";
    $aucResult = mysql_query($auctionSurvey) or die (mysql_error());

    while($auctionRow = mysql_fetch_assoc($aucResult)){
        echo $auctionRow['question_body'] . "<input type=text name=answer[]><BR>";?>
        <input type="hidden" name="id[]" value="<?php echo $auctionRow['question_id']?>">
        <?php
    }
    ?>

<input type="submit" name="submit" value="Submit">
</div>

    <!-- if selection is Competition -->
    <div id="2">
        <?php
        $compSurvey = "SELECT survey_id, question_body FROM questions 
                       WHERE survey_id='2'";
        $compResult = mysql_query($compSurvey) or die (mysql_error());
        while($compRow = mysql_fetch_assoc($compResult)){
            echo "$compRow[question_body]" . "<input type=text   name=><BR>";
        }
        ?>
        <input type="hidden" name="id" value="<?php echo "$compRow [question_id]"?>">
        <input type="submit" name="submit" value="Submit">
    </div>


    <!-- if selection is Gallery -->
    <div id="3">
        <?php
            $gallerySurvey = "SELECT survey_id, question_body FROM questions 
                              WHERE survey_id='3'";
            $galleryResult = mysql_query($gallerySurvey) or die  (mysql_error());
            while($galleryRow = mysql_fetch_assoc($galleryResult)){
                echo " $galleryRow[question_body]" . "<input   type=text name=answer><BR>";
            }
        ?>
        <input type="hidden" name="id" value="<?php echo "$galleryRow [question_id]"?>">
        <input type="submit" name="submit" value="Submit">
    </div>
</form>
</body>
</html>

<script>
document.getElementById('surveyCategory').onchange = function() {
var i = 1;
var myDiv = document.getElementById(i);
while(myDiv) {
    myDiv.style.display = 'none';
    myDiv = document.getElementById(++i);
}
document.getElementById(this.value).style.display = 'block';
};
</script>

You made several mistakes: 您犯了几个错误:

  1. possible multiple fields with the same name. 可能有多个具有相同名称的字段。
  2. Not using a loop to insert the data. 不使用循环插入数据。
  3. Didn't quote $id before using it in query. 在查询中使用$ id之前没有引用它。

First fix 首先修复

Add "[]" for answer: 添加“ []”作为答案:

        while($auctionRow = mysql_fetch_assoc($aucResult)){
            echo "$auctionRow[question_body]" . 
                 "<input type=\"text\" name=\"answer[]\"><BR>";
        }

Second fix 第二修复

Add a loop for every inserted answer, in case there are multiple: 如果有多个答案,则为每个插入的答案添加一个循环:

if (isset($_POST['submit'])){
    $id = $_POST['surveyCategory'];
    //escaping $id
    $id = mysql_real_escape_string($id);
    $questionID = $_POST['id'];
    $answers = $_POST['answer'];
    foreach($answers as $answer)
    {
        $answer = mysql_real_escape_string(htmlspecialchars($answer));
        mysql_query("INSERT INTO answers(survey_id, question_id, answer_body) VALUES   ('$id', '$questionID', '$answer')") or die (mysql_error());
    }

}

Also mysql_* functions are deprecated, consider switchign to mysqli or PDO. 另外不建议使用mysql_ *函数,请考虑切换到mysqli或PDO。

I have updated the answer.. 我已经更新了答案。

        <?php
        //database
        $connection = mysql_connect("localhost", "root", "") or die (mysql_error());
        mysql_select_db("survey" , $connection) or die (mysql_error());

        if(isset($_POST['submit'])){

        $surveyID =$_POST['surveyCategory'];

        for($i=0; $i<count($_POST['id']); $i++){
                $questionID = $_POST['id'][$surveyID][$i];
                $answer = mysql_real_escape_string(htmlspecialchars($_POST['answer'][$surveyID][$i]));
                mysql_query("INSERT INTO answers(survey_id, question_id, answer_body) VALUES   ('$surveyID', '$questionID', '$answer')") or die (mysql_error());
        }
        }



        ?>

        <style>
        div{
            display: none;
        }
        </style>


        <html>
        <body>
        <form name="displayQuestion" method="post">
            Survey Categories : 
            <select name="surveyCategory" id="surveyCategory">
            <option> Choose Survey Category </option>
            <?php
                $surveyQuery = "SELECT survey_id, survey_name FROM surveys";
                $result = mysql_query($surveyQuery) or die (mysql_error());
                while($menu=mysql_fetch_assoc($result)){
                echo '<option value="'.$menu['survey_id'].'">'.$menu['survey_name'].'</option>';              
                }
            ?>
            </select>

        <!-- if selection is auction -->
        <div id="1" style="display:none">
            <?php
            $auctionSurvey = "SELECT question_id, survey_id, question_body FROM questions 
                              WHERE survey_id='1'";
            $aucResult = mysql_query($auctionSurvey) or die (mysql_error());

            while($auctionRow = mysql_fetch_assoc($aucResult)){
                echo $auctionRow['question_body'] . "<input type=text name=answer[1][]><BR>";?>
                <input type="hidden" name="id[1][]" value="<?php echo $auctionRow['question_id']?>">
                <?php
            }
            ?>

        <input type="submit" name="submit" value="Submit">
        </div>

            <!-- if selection is Competition -->
            <div id="2">
                <?php
                $compSurvey = "SELECT survey_id, question_body FROM questions 
                               WHERE survey_id='2'";
                $compResult = mysql_query($compSurvey) or die (mysql_error());
                while($compRow = mysql_fetch_assoc($compResult)){
                    echo "$compRow[question_body]" . "<input type='text' name='answer[2][]'><BR>";?>
                    <input type="hidden" name="id[2][]" value="<?php echo $compRow['question_id']?>"><?php
                }
                ?>

                <input type="submit" name="submit" value="Submit">
            </div>


            <!-- if selection is Gallery -->
            <div id="3">
                <?php
                    $gallerySurvey = "SELECT survey_id, question_body FROM questions 
                                      WHERE survey_id='3'";
                    $galleryResult = mysql_query($gallerySurvey) or die  (mysql_error());
                    while($galleryRow = mysql_fetch_assoc($galleryResult)){
                        echo $galleryRow[question_body] . "<input   type='text' name='answer[3][]'><BR>";?>
                        <input type="hidden" name="id[3][]" value="<?php echo $galleryRow ['question_id']?>"><?php
                    }
                ?>

                <input type="submit" name="submit" value="Submit">
            </div>
        </form>
        </body>
        </html>

        <script>
        document.getElementById('surveyCategory').onchange = function() {
        var i = 1;
        var myDiv = document.getElementById(i);
        while(myDiv) {
            myDiv.style.display = 'none';
            myDiv = document.getElementById(++i);
        }
        document.getElementById(this.value).style.display = 'block';
        };
        </script>

just paste it and try 只需粘贴并尝试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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