简体   繁体   中英

Insert data from multiple textfields into MYSQL Database

Basically, I am able to display the DIVs based on the selection of drop box. 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. 1 row in the database is taken though.

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

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.

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

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