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>
From what I can tell you’ll only ever have 1 element in your $insertquestion array:
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:
And then in your PHP:
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.