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

时间:2013-12-16 02:32:47

标签: php html mysql

基本上,我可以根据选择的投递箱显示DIV。现在我的问题是: 当只有单个文本字段时,我能够插入数据。如果有2个文本字段,则提交成功,但无法在数据库中找到数据。但是,数据库中有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>

2 个答案:

答案 0 :(得分:1)

你犯了几个错误:

  1. 可能有多个同名字段。
  2. 不使用循环插入数据。
  3. 在查询中使用它之前没有引用$ id。
  4. 首先修复

    为答案添加“[]”:

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

    第二次修复

    为每个插入的答案添加一个循环,以防有多个:

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

    同样不推荐使用mysql_ *函数,请考虑将switchign转换为mysqli或PDO。

答案 1 :(得分: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'][$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>

只需粘贴即可尝试