Situation: I have a join table with columns testFK, questionFKand ordinal.
testFK | questionFK | ordinal
2 14 1
2 15 2
2 16 3
_____________________________
NEW 2 17 4
I want to add a new row to the table with testFK = 2and questionFK = 17, however, I want it to be ordinalautomatically created based on what is already in the table. Since the highest sequence number is 3, I want it to SQLautomatically generate 4.
I tried:
$stmt = $db->prepare('
SET @QOrdinal = (SELECT MAX(ordinal) FROM junc_test_question WHERE testFK = ?);
INSERT
INTO junc_test_question
(junc_test_question.testFK, junc_test_question.questionFK, junc_test_question.ordinal)
VALUES (?, ?, @QOrdinal);
');
$stmt->bind_param('iii', $this->testID, $this->testID, $question_id);
if($stmt->execute()) {
return true;
} else {
return false;
}
This works if I hardcode ordinalin, but I can't get SQLit to do it myself.
Any entry is welcome!
UPDATE:
Error I get
Fatal error: Call to a member function bind_param() on a non-object in \classes\Test.php on line 91
Therefore, I assume that it is SQLincorrect.