Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7721197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:51:47+00:00 2026-06-01T03:51:47+00:00

I have an application where the user can write up some questions and add

  • 0

I have an application where the user can write up some questions and add it in a table row in the application (each question goes into each row). After user has finished writing up their questions then they can add these questions into a database.

Now If the user only has one question to add in the database then this is fine because when I INSERT this in the database, it inserts the question in the database.

The problem though is that if the user has 2 or more questions to insert in the database, it only inserts the latest question in the database row and not both questions.

So for example if I have 1 question (what is 2+2) in the application table row, then this is what it will display below in the database:

SessionId    QuestionContent

SAS          What is 2+2

But if I have 2 question (what is 2+2 and what is 3+3) in the application table row, then this is what it will display below in the database:

 SessionId    QuestionContent

    SAS          What is 3+3

The above is incorrect as it only displays the latest question and not both questions. It should display this below in the database:

 SessionId    QuestionContent

    SAS          What is 2+2
    SAS          What is 3+3

So what my question is how can I Insert all the questions in the database like above in the database?

Below is the INSERT VALUES code I currently have:

     <?php

            mysql_connect('localhost',$username,$password);

            mysql_select_db($database) or die( "Unable to select database");

                     $insertquestion = array();

    foreach($_POST['questionText'] as $question)
    {
        $insertquestion[] = "' ". mysql_real_escape_string( $_SESSION['id'] ) . "' , ' ".     mysql_real_escape_string( $question ) . "'";
    }

      $questionsql = "INSERT INTO Question (SessionId, QuestionContent) 
      VALUES (" . implode('), (', $insertquestion) . ")";



    mysql_query($questionsql);

            mysql_close();

        ?>

Below is the full code for you to see how a question is added using javascript and html. Follow it carefully and you will understand how a question is appended or added into a table row:

        <script>

        function insertQuestion(form) {

        var $tbody = $('#qandatbl > tbody'); 
            var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
            var $question = $("<td class='question'></td>");

         $('#questionTextArea').each( function() {

        var $this = $(this);
        var $questionText = $("<textarea class='textAreaQuestion'></textarea>").attr('name',$this.attr('name')+"[]")
                       .attr('value',$this.val())

        $question.append($questionText);

        });


        $tr.append($question);
        $tbody.append($tr); 

        }

        </script>

        <body>

        <form id="QandA" action="insertQuestion.php" method="post" >

<h1>SESSION (<?php echo $_SESSION['id'] ?>)</h1>

        <table id="question">
        <tr>
            <td rowspan="3">Question:</td> 
            <td rowspan="3">
                <textarea id="questionTextArea" rows="5" cols="40" name="questionText"></textarea>
            </td>
        </tr>
        </table>

        <p><input id="addQuestionBtn" name="addQuestion" type="button" value="Add Question" onClick="insertQuestion(this.form)" /></p>

        <hr/>


        <table id="qandatbl" align="center">
        <thead>
        <tr>
            <th class="question">Question</th>
        </tr>
        </thead>
        <tbody>
        </tbody>
        </table>


        <p><input id="submitBtn" name="submitDetails" type="submit" value="Submit Details" /></p>

        </form> 

        </body>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:51:48+00:00Added an answer on June 1, 2026 at 3:51 am

    From what I can tell you’ll only ever have 1 element in your $insertquestion array:

    $insertquestion[] = "' ". mysql_real_escape_string( $_SESSION['id'] ) . "' , ' ". mysql_real_escape_string( $_POST['questionText'] ) . "'";
    

    This inserts one element. When you implode this array there’s only one element so it’s the same as $insertquestion[0].

    What does your HTML form look like that allows you to enter multiple questions? Are you using multiple form fields for that or putting multiple questions into one form field? If it’s the latter then you’ll first need to separate those questions and push each one into $insertquestion.

    EDIT
    Based on your HTML/JS code the problem is that you insert each new textarea with the same name. When the form is submitted, duplicate named elements are grouped and this results in only the last element with that name showing up in your $_POST array

    To fix this you can append “[]” to the name of the elements. By doing so, the $_POST array for that name will contain an array.

    So in your javascript code change it to this:

    var $questionText = $("<textarea class='textAreaQuestion'</textarea>").attr('name',$this.attr('name')+"[]").attr('value',$this.val())
    

    And then in your PHP:

    $insertquestion = array();
    
    foreach($_POST['questionText'] as $question)
    {
        $insertquestion[] = "' ". mysql_real_escape_string( $_SESSION['id'] ) . "' , ' ".     mysql_real_escape_string( $question ) . "'";
    }
    
      $questionsql = "INSERT INTO Question (SessionId, QuestionContent) 
      VALUES (" . implode('), (', $insertquestion) . ")";
    

    That will loop through the $_POST[‘questionText’] array and insert each element of that array (i.e. each question) into the $insertquestion array. After that you’re good to go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application where for each object the user can specify his own
I have a web application with users and their documents. Each user can have
Currently I have a web application where a user can use dropdown lists to
I have an application that shows a list of items. The user can click
I have an view-based application where the user can do a lot of customization
I have a flashlite3 application with navigation consisting of icons the user can browse
I have a Timestamp value that comes from my application. The user can be
My application will have a per machine (not per user) Startup shortcut. I can
I am having a number of controls in my application(which user can add to
I've some questions .. and I really need your help. I have an application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.