繁体   English   中英

PDO PHP更新仅更新1行

[英]PDO php update only updating 1 row

我的android项目中有一个增强的循环,该循环将对象数组循环到我的改造实例。

Retrofit创建一个JSON数组,该数组最终在我的更新操作中结束。 问题是,只有1行更新。

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"
}

我有以下更新查询

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(); 并检查每个变量,以确保它们保持了预期的值。

我相信问题出在我的更新查询中,但我不确定为什么或如何解决,邮递员没有给我任何错误-它只是默默地失败了!

任何帮助/建议,将不胜感激。

我设法找到了问题,并认为我会发布答案,以防将来对其他人有帮助。

在我的问题中,我发布了所有要发送的JSON数组,敏锐的眼睛可以注意到我的第一个数组总是更新的原因是因为没有空白IE

{
"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"
}

因此要解决,我在设置值IE之后简单地调用了.trim()

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

暂无
暂无

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

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