简体   繁体   English

PDO PHP更新仅更新1行

[英]PDO php update only updating 1 row

I have an enhanced loop in my android project that loops out an object array to my retrofit instance. 我的android项目中有一个增强的循环,该循环将对象数组循环到我的改造实例。

Retrofit creates a JSON array which eventually ends up at my update operation. Retrofit创建一个JSON数组,该数组最终在我的更新操作中结束。 The problem is, that only 1 row updates. 问题是,只有1行更新。

Retrofit will send the follow JSON array one by one to my update script Retrofit将以下JSON数组一一发送到我的更新脚本

{
"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. 我已经使用OkHttpClient来检查是否在我的Android程序中正确创建了JSON数组,然后将其发送到我的PDO文件中。

I have also used var_dump(); 我也用过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 在我的问题中,我发布了所有要发送的JSON数组,敏锐的眼睛可以注意到我的第一个数组总是更新的原因是因为没有空白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 但是在所有其他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"
}

Therefore to fix, I simply called .trim() after I set the values IE 因此要解决,我在设置值IE之后简单地调用了.trim()

String answer1 = et_answer1_edit.getText().toString().trim();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM