1

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>
5
  • How can data not be found in the database, when you do an insert? What data are you searching for? Commented Dec 16, 2013 at 2:38
  • You have an obvious error: possible multiple inputs with the same name: "answer" Commented Dec 16, 2013 at 2:38
  • If there are only 1 text field, data can be found. If I submit data from 2 texfields, no data is found, but 1 row is taken for that submission. Commented Dec 16, 2013 at 2:39
  • found or inserted? You want to insert 2 rows, don't you? Commented Dec 16, 2013 at 2:42
  • Ooops my bad. Yes I want to insert 2 rows. Currently, only 1 row is inserted and the answer_body field is empty. Commented Dec 16, 2013 at 2:43

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

11 Comments

surveyCategory is the name of my dropdown box
I have added my code for my dropdown in case it confuses you.
Yeah. I got these 2 errors when I click on Submit button Notice: Uninitialized string offset: 0 in C:\xampp\htdocs\assignment\displayQuestion.php on line 11 Notice: Uninitialized string offset: 0 in C:\xampp\htdocs\assignment\displayQuestion.php on line 12
Did you add a form tag and post method?
Yes I did. I added the form tag before I start the DIV and added post method for it.
|
1

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.

6 Comments

I got this error when I click on Submit. Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\assignment\displayQuestion.php on line 11
@user3103739 can you var_dump($answers); before foreach?
I got this error after adding that. string(0) "" Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\assignment\displayQuestion.php on line 13
@user3103739 For some reason, you are getting an empty string instead of array. Did you add [] for the answer field?
Yes I did. The answer field in the DIV.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.