我的android项目中有一个增强的循环,它将一个对象数组循环到我的改造实例。
Retrofit会创建一个JSON数组,最终会在我的更新操作中结束。问题是,只有1行更新。
Retrofit将逐个发送跟随JSON数组到我的更新脚本</ p>
{
"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"
}
我有以下更新查询
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;
}
}
这是我的变量中引用的表以及更新数据的位置。
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`)
);
我试图解决的问题
我已经使用OkHttpClient
来检查我的Android程序中是否正确创建了JSON数组,然后将其发送到我的PDO文件 - 它们就是这样。
我还使用了var_dump();
并检查了每个变量,以确保它们保持预期值,并且它们是。
我认为问题在于我的更新查询,但我不确定为什么或如何修复,Postman没有给我任何错误 - 它只是在沉默中失败!
任何帮助/建议都将不胜感激。
答案 0 :(得分:0)
我设法找到了问题,并且认为我会发布答案以防将来帮助其他人。
在我的问题中,我发布了所有被发送的JSON数组,敏锐的眼睛可以注意到我的第一个数组总是更新的原因是因为没有空格I.E
{
"answer": {
"answerString": "Answer1", <----Here is ok
"correct": "Incorrect",
"question_id": "1",
"unique_id_edit": "59b43c44a0f755.82885599" <------ Here is ok
},
"operation": "answer_edit"
}
然而,在我所有其他JSON数组上,我有一些空格
{
"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"
}
因此,为了修复,我在设置值I.E
之后简单地调用了.trim()String answer1 = et_answer1_edit.getText().toString().trim();