I have an enhanced loop in my android project that loops out an object array to my retrofit instance.
Retrofit creates a JSON array which eventually ends up at my update operation. The problem is, that only 1 row updates.
Retrofit will send the follow JSON array one by one to my update script
{
"answer": {
"answerString": "Answer1",
"correct": "Incorrect",
"question_id": "1",
"unique_id_edit": "59b43c44a0f755.82885599"
},
"operation": "answer_edit"
}
{
"answer": {
"answerString": " Answer2",
"correct": "Incorrect",
"question_id": "1",
"unique_id_edit": " 59b43c44a10653.76375270"
},
"operation": "answer_edit"
}
{
"answer": {
"answerString": " Answer3",
"correct": "Incorrect",
"question_id": "1",
"unique_id_edit": " 59b43c44a1b5c6.27290898"
},
"operation": "answer_edit"
}
{
"answer": {
"answerString": " Answer4",
"correct": "Incorrect",
"question_id": "1",
"unique_id_edit": " 59b43c44a2b765.62888841"
},
"operation": "answer_edit"
}
I have the following update query
public function editAnswer($unique_id_edit, $answerString, $correct, $question_id){
$sql = "UPDATE answer SET answerString = :answerString, correct = :correct WHERE unique_id = :unique_id AND question_id = :question_id";
// Prepare statement
$query = $this ->conn ->prepare($sql);
// execute the query
$query->execute(array(':unique_id' => $unique_id_edit, ':answerString' => $answerString,':correct' => $correct, ':question_id' => $question_id));
if($query){
var_dump();
return true;
} else {
return false;
}
}
This is the table that are referenced in my variables and where the data is updated.
CREATE TABLE answer(
answer_id int(11) NOT NULL AUTO_INCREMENT,
unique_id varchar(23) NOT NULL,
answerString varchar(50) NOT NULL,
correct varchar(20) NOT NULL,
question_id int (11) NOT NULL,
PRIMARY KEY (answer_id),
FOREIGN KEY (`question_id`)
REFERENCES `scratchcard`.`question` (`question_id`)
);
Things I've tried to solve
I have used OkHttpClient
to check that the JSON arrays are being created correctly in my Android program and then sent across to my PDO files - which they are.
I have also used var_dump();
and checked each variable to make sure they are holding their intended values, and they are.
I believe the problem lies in my update query but I am not sure why or how to fix and Postman is giving me no error - it just fails in silence!
Any help/advice would be appreciated.
I managed to find the problem, and thought I would post the answer in case it helps anyone else in future.
In my question, I posted all of the JSON arrays being sent across, a keen eye can notice that the reason why my first array always updated was because there was no white space IE
{
"answer": {
"answerString": "Answer1", <----Here is ok
"correct": "Incorrect",
"question_id": "1",
"unique_id_edit": "59b43c44a0f755.82885599" <------ Here is ok
},
"operation": "answer_edit"
}
However on all my other JSON arrays, I have some blank spaces
{
"answer": {
"answerString": " Answer2", <-----Blank space between the quotes
"correct": "Incorrect",
"question_id": "1",
"unique_id_edit": " 59b43c44a10653.76375270" <-----Blank space between the quotes
},
"operation": "answer_edit"
}
Therefore to fix, I simply called .trim() after I set the values IE
String answer1 = et_answer1_edit.getText().toString().trim();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.