I have a problem with my INSERT VALUES. The problem is when I try and insert question numbers. Lets say I try to insert 2 questions. Well for first question the question number (QuestionId) should be 1 and for second question it should be 2.
But the problem is that both questions display question number 3 when I INSERT VALUES. So what it is doing for every question is inserting what would be the next question number which in this example is '3' for all questions.
Below is the example of what it is displaying at the moment when I echo $questionsql for 2 questions:
INSERT INTO Question (QuestionId, QuestionContent) VALUES ('3','what is my name'), ('3','what is my age')
The above is incorrect. Below is what it should of echoed:
INSERT INTO Question (QuestionId, QuestionContent) VALUES ('1','what is my name'), ('2','what is my age')
So what I want to know is how can I display the correct question numbers for each question, it should add the correct question number in the correct order from question 1, 2 ,3 etc.
Below is the javascript code and form code where it appends a question into a table row. The user appends a question into a table row. When user adds first question it appends question number 1 and the question, when they append their second question it appends question number 2 and the question and etc.
<script>
function insertQuestion(form) {
var $tbody = $('#qandatbl > tbody');
var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
var $qid = $("<td class='qid'>" + qnum + "</td>");
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($qid);
$tr.append($question);
$tbody.append($tr);
}
++qnum;
$(".questionNum").text(qnum);
$(".num_questions").val(qnum);
</script>
<form id="QandA" action="insertQuestion.php" method="post" >
<table id="question">
<tr>
<th colspan="2">
Question Number <span class="questionNum">1</span>
<input type="hidden" class="num_questions" value="" name="numQuestion">
</th>
</tr>
<tr>
<td rowspan="3">Question:</td>
<td rowspan="3">
<textarea class="questionTextArea" rows="5" cols="40" name="questionText"></textarea>
</td>
</tr>
</table>
</form>
Below is the php code where it INSERT VALUES. How can I get it to display numQuestion correctly. I can't do a simple count loop because of the fact that I may have 2 exams so after creating questions for first exam, in second exam I need the question number to start at '1' again so I think it is better to correctly display the 'numQuestion'.
$i = 0;
$c = count($_POST['gridValues']);
$insertquestion = array();
for($i = 0; $i < $c; $i++ ){
switch ($_POST['gridValues'][$i]){
case "3":
$selected_option = "A-C";
break;
case "4":
$selected_option = "A-D";
break;
default:
$selected_option = "";
break;
}
$optionquery = "SELECT OptionId FROM Option_Table WHERE (OptionType = '". mysql_real_escape_string($selected_option)."')";
$optionrs = mysql_query($optionquery);
$optionrecord = mysql_fetch_array($optionrs);
$optionid = $optionrecord['OptionId'];
$insertquestion[] = "'".
mysql_real_escape_string( $_POST['numQuestion'] ) ."','".
mysql_real_escape_string( $_POST['questionText'][$i] ) ."','".
mysql_real_escape_string( $optionid ) ."'";
}
$questionsql = "INSERT INTO Question (SessionId, QuestionId, QuestionContent, QuestionMarks, OptionId)
VALUES (" . implode('), (', $insertquestion) . ")";
echo($questionsql);
mysql_close();