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

时间:2015-03-17 22:36:15

标签: php mysql pdo

我正在尝试使用PDO更新我的数据库,但它无法正常工作。我可以选择没有任何打嗝的数据,但更新只是不起作用。我没有收到任何错误消息,但在执行查询后,我的数据库中没有任何更改。

这是我的代码,这个方法是类的一部分所以定义了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;
        }
    }

$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);

谢谢!

1 个答案:

答案 0 :(得分:0)

更新语句用逗号分隔要更新的字段而不是ands:

$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';