Reputation: 1207
JSfiddle: http://jsfiddle.net/ybZvv/57/
I have a fiddle here where the user can append row and select answers for each row. To use the fiddle please follow steps below:
Now what I want to do is that for each answer in each row, they will contain it's own "AnswerId" and the answers will be inserted into the "Answer" column in the "Answer" table.
Below is what the table should look like following the jsfiddle answers:
Answer Table
AnswerId (auto) Answer
5 AB
6 ACE
The only problem I am getting with the above table is that it is not inserting the answers unser the "Answer" column.
Then I want to store the "AnswerId" from the "Answer" Table in the "Question" table. So as I have appended 2 rows, the "QuestionId" is 1 and 2 and below is what the "Question" Table should look like:
Question Table
QuestionId AnswerId
1 5
2 6
The above table is working fine.
Below is the error I am recieving:
My question is that by looking at the code below, how can the above error be fixed so that it is able to INSERT the answer under the "Answer" column?
Below is the php/mysqli code (I have commented the line where the error is occuring):
$i = 0;
$c = count($_POST['gridValues']); //Counts each appended row which works
for($i = 0; $i < $c; $i++ ){
$selected_answer = $_POST['value'];
$answersql = "INSERT INTO Answer (Answer)
VALUES (?)";
if (!$insertanswer = $mysqli->prepare($answersql)) {
// Handle errors with prepare operation here
}
$insertanswer->bind_param("s", $selected_answer);
$insertanswer->execute();
if ($insertanswer->errno) {
// Handle query error here
}
$insertanswer->close();
$lastID = $mysqli->insert_id;
$questionsql = "INSERT INTO Question (QuestionId, AnswerId)
VALUES (?, ?)";
if (!$insert = $mysqli->prepare($questionsql)) {
// Handle errors with prepare operation here
}
$insert->bind_param("ii", $_POST['numQuestion'][$i], $lastID);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
$insert->close();
}
?>
Below is the code where it successfully appends a question number for each row:
var qnum = 1;
var $qid = $("<td class='qid'></td>").text(qnum);
$('.num_questions').each( function() {
var $this = $(this);
var $questionNumber = $("<input type='hidden' class='num_questionsRow'>").attr('name',$this.attr('name')+"[]")
.attr('value',$this.val());
$qid.append($questionNumber);
++qnum;
$(".questionNum").text(qnum);
$(".num_questions").val(qnum);
$tr.append($qid);
Upvotes: 1
Views: 486
Reputation: 2780
I see issues on both client- and server-sides. I'll first address the client-side issues.
The input fields you create have names in the format: value[answerXRow]
, where X
is the selected answer character. If the user selects the same character across multiple answers, there will be multiple input fields with the same name (e.g. two value[answerARow]
fields based on your instructions). Moreover, there is no way for your PHP code to tell the difference between which value
is for which question, because they will all be submitted in the same value
array.
My suggestion is to use a multi-dimensional array in this format: value[n][]
, where n
is the question number. With this new setup, you should end up with the following input fields:
<input type="hidden" value="A" name="value[1][]">
<input type="hidden" value="B" name="value[1][]">
<input type="hidden" value="A" name="value[2][]">
<input type="hidden" value="C" name="value[2][]">
<input type="hidden" value="E" name="value[2][]">
Note that the selected value is encoded in the value
attribute. The name
attribute only contains the question to which the value belongs.
Now, on the server-side, you'll need to iterate over the $_POST['value']
array:
// $questionNumber is simply n in value[n][]
// $answers is an array of value attributes from <input>s for this question
foreach ($_POST['value'] as $questionNumber => $answers) {
// combine all the answers into a single string (e.g. ACE)
$selected_answer = implode('', $answers);
// continue onto inserting rows into the Answer and Questions tables
// ...
}
Upvotes: 1
Reputation: 18715
You're taking the values like they'e being posted as an array and they won't be, var_dump out the $_POST values to see what it's actually returning.
For the record, that code is an absolute mess. I'm not trying to be mean here, but your JS allows multiple answers to the same question. The select in the PHP is an utter waste and that entire section could be replaced with:
$selected_answer = $_POST['value'];
You're running a select to set the variable to the post value anyway...
Upvotes: 1