I have a php code below where it inserts questions and answer:
<?php
var_dump($_POST);
// Prepare your statements ahead of time
$questionsql = 'INSERT INTO Question (SessionId, QuestionId, QuestionContent) VALUES (?, ?, ?)';
if (!$insert = $mysqli->prepare($questionsql)) {
// Handle errors with prepare operation here
echo __LINE__ . ': ' . $mysqli->error;
}
$answersql = 'INSERT INTO Answer (SessionId, QuestionId, Answer) VALUES (?, ?, ?)';
if (!$insertanswer = $mysqli->prepare($answersql)) {
// Handle errors with prepare operation here
echo __LINE__ . ': ' . $mysqli->error;
}
//make sure both prepared statements succeeded before proceeding
if ($insert && $insertanswer) {
$sessid = $_SESSION['id'] . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '');
$c = count($_POST['numQuestion']);
for ($i = 0; $i < $c; $i++) {
switch ($_POST['gridValues'][$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;
}
$results = $_POST['value'];
foreach ($results as $id => $value) {
$answer = $value;
$insert->bind_param('sis', $sessid, $id, $_POST['questionText'][$i]);
$insert->execute();
if ($insert->errno) {
// Handle query error here
echo __LINE__ . ': ' . $insert->error;
break 2;
}
$lastID = $insert->insert_id;
foreach ($value as $answer) {
$insertanswer->bind_param('sis', $sessid, $lastID, $answer);
$insertanswer->execute();
if ($insertanswer->errno) {
// Handle query error here
echo __LINE__ . ': ' . $insertanswer->error;
break 3;
}
}
}
}
//close your statements at the end
$insertanswer->close();
$insert->close();
}
Problem is though it is giving me this error:
Warning: mysqli_stmt::execute(): (23000/1062): Duplicate entry
‘RXT-1’ for key ‘PRIMARY’ in /…/ on line 92 97: Duplicate entry
‘RXT-1’ for key ‘PRIMARY’
Now I have composite keys for “SessionId” and “QuestionId” but I don’t believe the problem is this because below is what the table is displaying:
Question Table:
SessionId(PK) QuestionId(PK) QuestionContent
RZC 1 What is 2+2 and 3+3?
RZC 2 What is 2+2 and 3+3?
The problem I believe is that it is displaying the same question from question 1 into both table rows. Question 2 should be a different question (What is 5+5 and 6+6?)
So my question is that why is it displaying the same question in both rows and how can I change my code to fix it?
Below is the what the var_dump($_POST) is displaying: The ones associating with this problem are ["numQuestion"] and ["questionText"].
array(8) {
["numberAnswer"]=> array(2) {
[0]=> string(1) "2"
[1]=> string(1) "3"
}
["numQuestion"]=> array(2) {
[0]=> string(1) "1"
[1]=> string(1) "2"
}
["questionText"]=> array(2) {
[0]=> string(20) "What is 2+2 and 3+3?"
[1]=> string(41) "Which three items will you find in a car?"
}
["gridValues"]=> array(2) {
[0]=> string(1) "4"
[1]=> string(2) "10"
}
["reply"]=> array(2) {
[0]=> string(8) "multiple"
[1]=> string(8) "multiple"
}
["textWeight"]=> array(2) {
[0]=> string(1) "5"
[1]=> string(1) "5"
}
["submitDetails"]=> string(14) "Submit Details"
["value"]=> array(2) {
[1]=> array(2) {
[0]=> string(1) "A"
[1]=> string(1) "C"
}
[2]=> array(3) {
[0]=> string(1) "A"
[1]=> string(1) "B"
[2]=> string(1) "D"
}
}
}
If you want to insert the question first, and after that all answers, your question query shouldn’t be inside the answer insert loop, otherwise it will be executed for every answer found, but not altered.
Bad:
Better: