Struggling to insert values correctly into database - php

I want 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.
So what the above inputs are stating is this:
question 1: answer: A
question 1: answer: B
question 2: answer: A
question 2: answer: C
question 2: answer: E
I want to insert these details into "Question" and "Answer" database tables below:
Question Table:
SessionId QuestionId
MUL 1
MUL 2
Answer Table:
AnswerId (auto) SessionId QuestionId Answer
1 MUL 1 A
2 MUL 1 B
3 MUL 2 A
4 MUL 2 C
5 MUL 2 E
Now I have attempted writing the mysqli/php code below to insert these values into the database but I am receiving errors and failing badly in wanting to acheive what I want to achieve. I need help being able to correctly insert the correct values in the relevant tables.
Below is the php/mysqli code:
var_dump($_POST);
$i = 0;
$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;
}
*/ needed later on when I insert grid values
$results = $_POST['value'];
foreach($results as $id => $value) {
$answer = implode(':', $value);
$questionsql = "INSERT INTO Question (SessionId, QuestionId)
VALUES (?, ?)";
$sessid = $_SESSION['id'] . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '');
if (!$insert = $mysqli->prepare($questionsql)) {
// Handle errors with prepare operation here
}
$insert->bind_param("si", $sessid, $id);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
$insert->close();
$lastID = $id->insert_id;
foreach($value as $answer) {
$answersql = "INSERT INTO Answer (SessionId, QuestionId, Answer)
VALUES (?, ?)";
if (!$insertanswer = $mysqli->prepare($answersql)) {
// Handle errors with prepare operation here
}
$insertanswer->bind_param("sis" $sessid, $lastID, $answer);
$insertanswer->execute();
if ($insertanswer->errno) {
// Handle query error here
}
$insertanswer->close();
}
}
}
The var_dump($_POST) outputs this below:
array(3) {
["numQuestion"]=> array(2) {
[0]=> string(1) "1"
[1]=> string(1) "2" }
["submitDetails"]=> string(14) "Submit Details"
["value"]=> array(4) {
["answerARow"]=> string(1) "A"
["answerCRow"]=> string(1) "C"
["answerBRow"]=> string(1) "B"
["answerERow"]=> string(1) "E" }
}
Below are the errors I am receiving and the line of code each error is linked to:
Warning: implode(): Invalid arguments passed in /.../ on line 226
$answer = implode(':', $value);
Notice: Trying to get property of non-object in /.../ on line 250
$lastID = $id->insert_id;
Warning: Invalid argument supplied for foreach() in /.../ on line 252
foreach($value as $answer) {
Warning: mysqli_stmt::execute(): (23000/1062): Duplicate entry
'MUL-0' for key 'PRIMARY' in /.../ on line 242
The Above Error Points to the $insert query
MORE INFO:
The structure I really want to acheive after posting the text inputs is below:
array(2) {
["numQuestion"]=> array(2) {
[0]=> string(1) "1"
[1]=> string(1) "2"
}
["submitDetails"]=> string(14) "Submit Details"
["1"] => array(2) {
[0] => string(1) "A"
[1] => string(1) "C"
}
["2"] => array(2) {
[0] => string(1) "A"
[1] => string(1) "B"
[2] => string(1) "E"
}
}
When the user posts the text input values, it should insert the question numbers and the answers which belongs to their relevant question number.

A few things:
$result / $_POST['value'] is a one-dimensional array, so $value in your foreach
loop is a string that you cannot use implode on;
$lastID = $id->insert_id; should be $insert->insert_id and I'm not sure if you can close the connection already before you call
that statement;
$value is a string (see first point) so you cannot use foreach there (third warning).

Related

How do I insert an array of id into database MySQL?

I'm trying to insert an array of IDs into a database table, but at the moment it only inserts 1 row when it's meant to do multiple rows. My array of IDs contains just (filmid) Does anyone know what the problem is?
$pm = "2";
$id = "2";
if($stmt1->execute())
{
$films=array();
foreach($_SESSION['products'] as $key=>$product)
{
array_push($films, $product['filmid']);
$f_id = implode(",", $films);
$stmt2 = $this->conn->prepare("INSERT INTO `filmPurchase` (`fpid`, `payid`, `filmid`, `shopid`, `custid`, `price`) VALUES (NULL, :id, :f_id, :pm, :pm, '8')");
$stmt2->bindParam('pm',$pm);
$stmt2->bindParam('id',$id);
$stmt2->bindParam('f_id',$f_id);
$stmt2->execute();
}
}
I've tried to loop over the array with:
foreach($_SESSION['products'] as $key=>$product)
{
var_dump($key);
var_dump($product);
}
This is what outputted:
int(29) array(4) { ["qty"]=> int(1) ["filmtitle"]=> string(45) "The Lord of
the Rings: The Return of the King" ["filmid"]=> string(2) "29" ["price"]=>
float(6.99) }
If your placeholder is :id and :pm (as in prepare()) then you must use :id in bindParam()
See php documentation

It does not do an insert if null value

I am trying to insert each answer for each question in the database. Problem is that there is a possibility that a question may have no answers, so I tried the code below but it does not insert a db row if a question has no answer, what I was trying to do is that if no answer then display the string No Answer under the Answer column for that question:
$answersql = "INSERT INTO Answer (QuestionId, Answer)
VALUES (?, ?)";
if (!$insertanswer = $mysqli->prepare($answersql)) {
// Handle errors with prepare operation here
echo __LINE__.': '.$mysqli->error;
}
if( $insert && $insertanswer)
{
$c = count($_POST['numQuestion']);
$question_ids = array();
for($i = 0; $i < $c; $i++ )
{
... //Question INSERT goes here
$questionId = $mysqli->insert_id;
$question_ids[$questionNo] = $questionId;
}
$results = $_POST['value'];
foreach($results as $id => $value)
{
$answer = $value;
$quesid = (int)$question_ids[$id];
foreach($value as $answer)
{
if($answer == '' || $answer === null){
$answer = 'No Answer';
}
$insertanswer->bind_param("is", $quesid, $answer);
$insertanswer->execute();
if ($insertanswer->errno) {
// Handle query error here
echo __LINE__.': '.$insertanswer->error;
break 7;
}
}
}
//close your statements at the end
$insertanswer->close();
}
The ['value'] comes from an input:
var $newBtn = $(("<input class='answerBtnsRow answers' type='button' style='display:%s;' onclick='btnclick(this, " + gQuestionIndex + ");' />").replace('%s', $this.is(':visible') ? 'inline-block' : 'none')).attr('name', "value[" + gQuestionIndex + "][]").attr('value', $this.val()).attr('class', $this.attr('class')).attr('id', $this.attr('id') + 'Row');
UPDATE:
Below is the SHOW CREATE TABLE for the Answer Table:
CREATE TABLE `Answer` (
`AnswerId` int(10) NOT NULL AUTO_INCREMENT,
`QuestionId` int(10) NOT NULL,
`Answer` varchar(10) DEFAULT NULL,
PRIMARY KEY (`AnswerId`)
) ENGINE=InnoDB AUTO_INCREMENT=280 DEFAULT CHARSET=utf8
Below is a var dump where if I set question 1 to have answers B and C and set question 2 to have no answers at all, it outputs the following:
var_dump($question_ids);
var_dump($results);
array(2) {
[1]=> int(247)
[2]=> int(248)
}
array(1) {
[1]=> array(2) {
[0]=> string(1) "B"
[1]=> string(1) "C" }
}
You can tell from your var_dumps that you have two questions, but only one set of answers. your design flaw doesn't link the answers to the questions, from what I can see.
no second index in $results, means no second set of answers, therefore no insert ...
but if you have three questions, and two answer sets, do those answers belong to question 1&2 or 2&3, or 1&3. I am not see how you mach your question id to the result set.

How to insert database rows if there are no answers

I am trying to insert each answer for each question in the database. Problem is that there is a possibility that a question may have no answers, so I tried the code below but it does not insert a db row if a question has no answer, what I was trying to do is that if no answer then display the string No Answer under the Answer column for that question:
$answersql = "INSERT INTO Answer (QuestionId, Answer)
VALUES (?, ?)";
if (!$insertanswer = $mysqli->prepare($answersql)) {
// Handle errors with prepare operation here
echo __LINE__.': '.$mysqli->error;
}
if( $insert && $insertanswer)
{
$c = count($_POST['numQuestion']);
$question_ids = array();
for($i = 0; $i < $c; $i++ )
{
... //Question INSERT goes here
$questionId = $mysqli->insert_id;
$question_ids[$questionNo] = $questionId;
}
$results = $_POST['value'];
foreach($results as $id => $value)
{
$answer = $value;
$quesid = (int)$question_ids[$id];
foreach($value as $answer)
{
if($answer == '' || $answer === null){
$answer = 'No Answer';
}
$insertanswer->bind_param("is", $quesid, $answer);
$insertanswer->execute();
if ($insertanswer->errno) {
// Handle query error here
echo __LINE__.': '.$insertanswer->error;
break 7;
}
}
}
//close your statements at the end
$insertanswer->close();
}
The ['value'] comes from an input:
var $newBtn = $(("<input class='answerBtnsRow answers' type='button' style='display:%s;' onclick='btnclick(this, " + gQuestionIndex + ");' />").replace('%s', $this.is(':visible') ? 'inline-block' : 'none')).attr('name', "value[" + gQuestionIndex + "][]").attr('value', $this.val()).attr('class', $this.attr('class')).attr('id', $this.attr('id') + 'Row');
Below is the SHOW CREATE TABLE for the Answer Table:
CREATE TABLE `Answer` (
`AnswerId` int(10) NOT NULL AUTO_INCREMENT,
`QuestionId` int(10) NOT NULL,
`Answer` varchar(10) DEFAULT NULL,
PRIMARY KEY (`AnswerId`)
) ENGINE=InnoDB AUTO_INCREMENT=280 DEFAULT CHARSET=utf8
Below is a var dump where if I set question 1 to have answers B and C, question 2 to have no answers, question 3 to have answer B and question 4 to have no answer, it outputs the following from var dump:
var_dump($question_ids);
var_dump($results);
array(4) {
[1]=> int(265)
[2]=> int(266)
[3]=> int(267)
[4]=> int(268)
}
[1]=> array(2) {
[0]=> string(1) "B"
[1]=> string(1) "C"
}
[3]=> array(1) {
[0]=> string(1) "B"
}
So it outputs those set of answers in questions 1 and 3. But does not post No Answer for question 2 and 4.
If all questions had an answer as thisL
question 1: B C
question 2: A
question 3: B
question 4: A C
Then the var dump displays this in var dump:
array(4) {
[1]=> int(277)
[2]=> int(278)
[3]=> int(279)
[4]=> int(280)
}
array(4) {
[1]=> array(2) {
[0]=> string(1) "B"
[1]=> string(1) "C" }
[2]=> array(1) {
[0]=> string(1) "A"
}
[3]=> array(1)
{
[0]=> string(1) "B"
}
[4]=> array(2)
{
[0]=> string(1) "A"
[1]=> string(1) "C"
} }}
As you can see for each question is inserts the relevant answers. So the issu is if the question has no answers that it does not insert a db row and state No Answer. My question is how to include a No Answerfor each question in the db that has no answers selected in those questions?
Simply do not insert anything into Answer table when there is no answer. Rest of your solution is basically correct.
When displaying questions with answer, check if there is answer (empty select result or null in left joined column) to show and if not, show "No answer". But there is no "No answer" in database.
When your code get to $results = $_POST['value'];, $question_ids is populated with the ids of questions that must be included in the table.
My suggestion is to remove elements from this array right after including the first answer to the related question. This way, after the foreach($results as $id => $value) loop, this array will contain only questions without explicit answers. The next step is just to include these pseudo-answer "No answer" in the DB.
The relevant code for you (inserted lines are //* commented):
$notAnswered = $question_ids; //* Make a copy
$results = $_POST['value'];
foreach ($results as $id => $value) {
$answer = $value;
$quesid = (int)$question_ids[$id];
$pos = array_search($quesid, $notAnswered); //* Search for it
if ($pos !== false) //* It's in the array
array_splice($notAnswered, $pos, 1); //* Delete it from the array
foreach ($value as $answer) {
$insertanswer->bind_param("is", $quesid, $answer);
$insertanswer->execute();
if ($insertanswer->errno) {
// Handle query error here
echo __LINE__.': '.$insertanswer->error;
break 7;
}
}
}
//* Insert 'No Answer' for each question not answered
foreach ($notAnswered as $id) {
$insertanswer->bind_param('is', $id, 'No Answer');
$insertanswer->execute();
if ($insertanswer->errno) {
// Handle query error here
echo __LINE__.': '.$insertanswer->error;
break 7;
}
}
// Close your statements at the end
$insertanswer->close();
One important thing to note here: array_splice is very slow in PHP for big arrays (>100 elements). But I don't think this is the case here.

It is not inserting questionid in answer db table

I am having trouble being able to insert QuestionId into the Answer Table.
The Question inserts with no problem, but I am trying to retrieve the QuestionId from the Question Table which is an auto increment into the Answer Table by finding the SessionId and QuestionNo that particular QuestionId belongs to. Instead it keeps displaying 0 for QuestionId when inserted into Answer Table.
Now I know the query which tries to perform the select for each QuestionId from the Question Table is correct as this has been tested. I think the problem is where I have placed my code but I am not sure?
Here are the db tables:
Question Table
QuestionId (auto) SessionId QuestionNo
4 2 1
5 2 2
6 2 3
Answer Table at moment:
AnswerId (auto) QuestionId Answer
7 0 A
8 0 C
9 0 A
10 0 B
11 0 True
What Answer Table should look like:
AnswerId (auto) QuestionId Answer
7 4 A
8 4 C
9 5 A
10 5 B
11 6 True
Below is the code:
$questionsql = "INSERT INTO Question (SessionId, QuestionNo)
VALUES (?, ?)";
if (!$insert = $mysqli->prepare($questionsql)) {
// Handle errors with prepare operation here
echo __LINE__.': '.$mysqli->error;
}
$answersql = "INSERT INTO Answer (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++ )
{
$insert->bind_param("ii", $sessionid, $_POST['numQuestion'][$i]);
$insert->execute();
if ($insert->errno)
{
// Handle query error here
echo __LINE__.': '.$insert->error;
break 1;
}
}
$results = $_POST['value'];
foreach($results as $id => $value)
{
$answer = $value;
$lastID = $id;
$questionidquery = "SELECT QuestionId FROM Question WHERE (QuestionNo = ? AND SessionId = ?)";
if (!$questionidstmt = $mysqli->prepare($questionidquery)) {
// Handle errors with prepare operation here
echo __LINE__.': '.$mysqli->error;
}
// Bind parameter for statement
$questionidstmt->bind_param("ii", $lastID, $sessionId);
// Execute the statement
$questionidstmt->execute();
if ($questionidstmt->errno)
{
// Handle query error here
echo __LINE__.': '.$questionidstmt->error;
break 2;
}
// This is what matters. With MySQLi you have to bind result fields to
// variables before calling fetch()
$questionidstmt->bind_result($quesid);
// This populates $optionid
$questionidstmt->fetch();
$questionidstmt->close();
foreach($value as $answer)
{
$insertanswer->bind_param("is", $quesid, $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();
}
?>
UPDATE:
Results from var_dump($_POST);
array(8) {
["numberAnswer"]=> array(2) {
[0]=> string(1) "1"
[1]=> string(1) "1"
}
["numQuestion"]=> array(2)
{
[0]=> string(1) "1"
[1]=> string(1) "2"
}
["questionText"]=> array(2) {
[0]=> string(12) "What is 2+2?"
[1]=> string(12) "What is 4+4?"
}
["gridValues"]=> array(2) {
[0]=> string(1) "4"
[1]=> string(1) "4"
}
["reply"]=> array(2) {
[0]=> string(6) "single"
[1]=> string(6) "single"
}
["textWeight"]=> array(2) {
[0]=> string(1) "5"
[1]=> string(1) "5"
}
["submitDetails"]=> string(14) "Submit Details" ["value"]=> array(2) {
[1]=> array(1) {
[0]=> string(1) "B"
}
[2]=> array(1) {
[0]=> string(1) "D"
}
}
}
Right now your QuestionID is coming from $_POST['value'], which is probably not what you want... you need to get the automatically generated QuestionID from mysql after each question is inserted.
You can retrieve the last generated ID using $mysqli->insert_id. Do this after each call to $insert->execute();, then use those IDs when you go to insert your answers.
Edit 1:
You should be using $mysqli->insert_id, because insert_id is only valid on a connection object (yours is called $mysqli).
You also need to assign the insert_id to something. I'm having trouble following your code but I think what you want is to store question IDs in an array for later use, something like this:
$question_ids[$i] = $mysqli->insert_id
I'm at a loss for what you're trying to do after this, I guess you pull in some answers with a corresponding question number but... perhaps if you can post the output of var_dump($_POST) we will understand a bit better. In any case you will need some kind of a loop that inserts the answers for each question, and you can (hopefully) use the array $question_ids[$i] to retrieve the QuestionIDs.
Edit 2:
Here is some sample code (untested), try to get this working before getting fancy with $_POST and $_SESSION
<?php
$questions = array(
array(2, 1),
array(2, 2),
# etc
);
$answers = array(
array(A, C),
array(A, B),
);
$questionsql = "INSERT INTO Question (SessionId, QuestionNo) VALUES (?, ?)";
if (!$insert = $mysqli->prepare($questionsql)) {
die('couldn\'t prepare statement 1');
}
$answersql = "INSERT INTO Answer (QuestionId, Answer) VALUES (?, ?)";
if (!$insertanswer = $mysqli->prepare($answersql)) {
die('couldn\'t prepare statement 2');
}
$c = count($questions);
for($i = 0; $i < $c; $i++)
{
$insert->bind_param("ii", $questions[$i][0], $questions[$i][1]);
$insert->execute();
if ($insert->errno)
{
die("Error inserting question $i");
}
$lastID = $mysqli->insert_id;
foreach ($ans in $answers[$i])
{
$insertanswer->bind_param("is", $lastID, $ans);
$insertanswer->execute();
if ($insertanswer->errno) {
die("Error inserting answer to question $i");
}
}
}
$insertanswer->close();
$insert->close();
?>

How to insert correct values in database [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a jsfiddle here: http://jsfiddle.net/ybZvv/58/
Please follow steps in fiddle:
1: When you open fiddle, click on "Add Question" button twice, this will append 2 rows.
2: In first row select answer buttons "A" and "C", in second row select answer buttons "A", "B" and "E". The text input values for each answer button selected is displayed underneath.
Now what I want to do is post the question number and the answer values into the database.
The database should look like this below when posted:
Question Table:
QuestionId (Question Number)
1
2
Answer Table:
AnswerId (auto) QuestionId Answer
1 1 A
2 1 C
3 2 A
4 2 B
5 2 E
What my question is that how do I post the answers and the correct question numbers in my mysqli code below so that it inserts those Answers and Question Numbers in the "Question" and "Answer" Tables?
Below I have set up the mysqli/php code, but it needs retweaked so it can insert the answers and the relevant question numbers correctly.
$i = 0;
$c = count($_POST['numQuestion']); //count number of rows
for($i = 0; $i < $c; $i++ ){
$questionsql = "INSERT INTO Question (QuestionId)
VALUES (?)";
if (!$insert = $mysqli->prepare($questionsql)) {
// Handle errors with prepare operation here
}
$insert->bind_param("i", $_POST['numQuestion'][$i]);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
$insert->close();
$lastID = $mysqli->insert_id;
$answersql = "INSERT INTO Answer (QuestionId, Answer)
VALUES (?, ?, ?)";
if (!$insertanswer = $mysqli->prepare($answersql)) {
// Handle errors with prepare operation here
}
$insertanswer->bind_param("is", $lastID, $_POST['value'][$i]);
$insertanswer->execute();
if ($insertanswer->errno) {
// Handle query error here
}
$insertanswer->close();
}
I have done a var_dump($_POST) for the above scenario and this is what it outputs:
array(2) {
["numQuestion"]=> array(2) {
[0]=> string(1) "1"
[1]=> string(1) "2"
}
["submitDetails"]=> string(14) "Submit Details"
["value"]=> array(4) {
["answerARow"]=> string(1) "A"
["answerCRow"]=> string(1) "C"
["answerBRow"]=> string(1) "B"
["answerERow"]=> string(1) "E"
}
}
I am receiving 2 errors which are identical:
Warning: mysqli_stmt::execute(): (23000/1048): Column 'Answer' cannot
be null in /.../ on line 257 Warning: mysqli_stmt::execute():
(23000/1048): Column 'Answer' cannot be null in /.../ on line 257
UPDATE:
I have updated fiddle to include the multi-dimensional array, sorry I forgot to put it in but the line of code for this is below:
var $newBtn = $(("<input class='answerBtnsRow answers' type='button' style='display:%s;' onclick='btnclick(this, " + gQuestionIndex + ");' />").replace('%s',$this.is(':visible')?'inline-block':'none')).attr('name', "value[" + gQuestionIndex + "][]").attr('value', $this.val()).attr('class', $this.attr('class')).attr('id', $this.attr('id')+'Row');
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.
It seems like the $POST does not have all the information, i suggest you to change the post structure to relation the question to the answer, something like this:
array(2) {
["numQuestion"]=> array(2) {
[0]=> string(1) "1"
[1]=> string(1) "2"
}
["submitDetails"]=> string(14) "Submit Details"
["question_1"] => array(2) {
[0] => string(1) "A"
[1] => string(1) "C"
}
["question_2"] => array(2) {
[0] => string(1) "A"
[1] => string(1) "B"
[2] => string(1) "E"
}
}
Now, if the post would have that structure, you could do something like this to insert the data:
$numQuestionArray = $_POST["numQuestion"];
for($i =0; $i < count($numQuestionArray);$i++)
{
$questionId = $numQuestionArray[$i];
$questionsql = "INSERT INTO Question (QuestionId) VALUES (?)";
if (!$insert = $mysqli->prepare($questionsql)) {
// Handle errors with prepare operation here
}
$insert->bind_param("i", $questionId);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
$insert->close();
$lastID = $mysqli->insert_id;
$questionAnswerPostStr = "question_".$questionId;
$answerArray = $_POST[$questionAnswerPostStr];
for($j =0; $j < count($numQuestionArray);$j++)
{
$answer = $numQuestionArray[$j];
$answersql = "INSERT INTO Answer (QuestionId, Answer) VALUES (?, ?)";
if (!$insertanswer = $mysqli->prepare($answersql)) {
// Handle errors with prepare operation here
}
$insertanswer->bind_param("is", $lastID, $answer);
$insertanswer->execute();
if ($insertanswer->errno) {
// Handle query error here
}
$insertanswer->close();
}
}
However, i still think the question_id should be auto incremented on the table instead of inserting the $POST value.

Categories