I have 2 problems with my code below:
Problem 1:
For questionId, it is not displaying the correct question number. Lets say I have 2 questions. Then for question 1, questionId should be '1' but instead it displays '3' Another example is that if I have 7 questions, then for question 1, questionId should be '1' but it displays '8' instead.
How can this be fixed?
Problem 2:
My $questionsql is only echoing 1 question, even if I have multiple questions to echo. What am I doing wrong and can I show all the questions rather than only 1 question.
Example:
If I have 2 questions which is:
Question 1:
SessionId QuestionId QuestionContent OptionId
ABV 1 What is my name? O6
Question 2:
SessionId QuestionId QuestionContent OptionId
ABV 2 What is my age? O9
Then in the echo $questionsql it should display:
INSERT INTO Question (SessionId, QuestionId, QuestionContent, OptionId) VALUES ('ABV' ,'1','What is my name?','O6'), ('ABV' ,'2','What is my age?','O9')
But it is not displaying the above, instead it is displaying this below which is incorrect as it displays wrong question number and it displays only 1 question which is the latest question added.
INSERT INTO Question (SessionId, QuestionId, QuestionContent, OptionId) VALUES ('ABV' ,'3','What is my age?','O9')
UPDATE
Below is code, how can the 2 problems be fixed:
foreach($_POST['questionText'] as $i => $question)
{
$insertquestion = array();
$options[] = $_POST['gridValues'];
switch ($options[$i]){
case "3":
$selected_option = "A-C";
break;
case "4":
$selected_option = "A-D";
break;
case "5":
$selected_option = "A-E";
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( $_SESSION['id'] ) . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '') ."' ,'". mysql_real_escape_string( $_POST['num_questions'] ) ."','". mysql_real_escape_string( $question ) ."','". mysql_real_escape_string( $optionid ) ."'";
$questionsql = "INSERT INTO Question (SessionId, QuestionId, QuestionContent, OptionId)
VALUES (" . implode('), (', $insertquestion) . ")";
$i++;
}
echo($questionsql);
Below is the form code. How it works is the user types in a question in the textarea ('name='questionText') and types in an option (name='gridValues') and then they append them two in a table row (table in the form which id='qandatbl'). This is the question 1. Then they do the same again for second question, then third and etc. Please look at this carefully, it is easy to follow :)
You are setting $i=0;
just before the switch
statement, and inside your foreach
loop, so you end up creating just the first element in your arrays.
No worries! I do this all the time!
Okay, basically there are two problems here. The first is that on every cycle of your primary loop, you're resetting $i
. You need to make it the key way you find the value you want:
foreach($_POST['questionText'] as $i => $question)
{
$insertquestion = array();
$options[] = $_POST['gridValues'];
switch ($options[$i]){
...
I'm assuming here that your gridValues
input and your questionText
input correspond to one another, i.e. for the first gridValue you also want the first questionText.
The second thing which seems wrong is that you're assembling your SQL query as an array. What you need to do is make a separate query for each iteration of your loop:
$insertquestion = "'". mysql_real_escape_string( $_SESSION['id'] ) . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '') ."' ,'". mysql_real_escape_string( $_POST['num_questions'] ) ."','". mysql_real_escape_string( $question ) ."','". mysql_real_escape_string( $optionid ) ."'";
$questionsql = "INSERT INTO Question (SessionId, QuestionId, QuestionContent, OptionId)
VALUES (" . implode('), (', $insertquestion) . ")";
$i++;
}