简体   繁体   English

PDO不会更新数据库中的数据,为什么?

[英]PDO won't update my data in the database, why?

I am trying to update my database using PDO and it just won't work. 我正在尝试使用PDO更新我的数据库,但它行不通。 I can select data without any hiccups, but updating just won't work. 我可以毫无困难地选择数据,但是更新将无法进行。 I get no error message, but after executing the query nothing changes in my database. 我没有收到错误消息,但是执行查询后,数据库中没有任何变化。

Here is my code, this method is part of a class so pdo is defined but I won't paste the whole code into here, just the important bits: 这是我的代码,此方法是类的一部分,因此定义了pdo,但我不会将整个代码粘贴到此处,只是将重要部分粘贴到了这里:

public function modify($q, $data){
        try{
            $stmt = $this->pdo->prepare($q);
            $stmt->execute($data);
            return true;
        }
        catch(PDOException $e){
            $this->error = $e->getMessage();
            return false;
        }
    }

And: 和:

$question = new Question(base64_decode($_POST['value']), $_POST['question'], $_POST['code'], $_POST['number'], $_POST['answer1'],
    $_POST['answer2'], $_POST['answer3'], $_POST['answer4'], $_POST['correct_answer']);

$array = [
    ':qid' => $question->qid,
    ':question' => $question->question,
    ':code' => $question->code,
    ':number' => $question->number ,
    ':answer1' => $question->answer1,
    ':answer2' => $question->answer2,
    ':answer3' => $question->answer3,
    ':answer4' => $question->answer4,
    ':correct_answer' => $question->correct_answer#9
];

$q = 'UPDATE questions SET question = :question AND code = :code AND number = :number AND
        answer_1 = :answer1 AND answer_2 = :answer2 AND answer_3 = :answer3 AND answer_4 = :answer4
        AND correct_answer = :correct_answer WHERE question_id = :qid';

$db->query('SET NAMES UTF8');
$db->modify($q, $array);

Thanks! 谢谢!

Update statments separate the fields to be updated with commas not ands: 更新语句用逗号“ and”分隔要更新的字段:

$q = 'UPDATE questions SET question = :question, code = :code, 
       number = :number, answer_1 = :answer1, answer_2 = :answer2,
        answer_3 = :answer3, answer_4 = :answer4,
        correct_answer = :correct_answer 
      WHERE question_id = :qid';

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

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