我有以下PHP代码:

try{
    $stmt = $db->prepare('SELECT nume,prenume FROM candidati');
    $stmt->execute();
    while($row = $stmt->fetch(PDO::FETCH_OBJ)){
        $aux = $row->nume.' '.$row->prenume;
        $st = $db->prepare('SELECT COUNT(id) AS total FROM votanti WHERE consiliul_local=?');
        $st->execute(array($aux));
        while($r = $st->fetch(PDO::FETCH_OBJ)){
            $s = $db->prepare("INSERT INTO rezultate SET obtinute=:o WHERE nume=:n AND prenume=:p");
            $s->bindParam(':o',$r->total,PDO::PARAM_INT);
            $s->bindParam(':n',$row->nume,PDO::PARAM_STR);
            $s->bindParam(':p',$row->prenume,PDO::PARAM_STR);
            $s->execute();
        }
    }
} catch(PDOException $e){
    echo $e->getMessage();
}


和以下类:

class DB{
    public static function connect($engine,$host,$user,$pass,$name){
        try{
            $dbh = new PDO("$engine:host=$host;dbname=$name;charset=utf8",$user,$pass);
            $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
            $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
            return $dbh;
        } catch(PDOException $e){
            echo $e->getMessage();
        }
    }
}


但是,当我执行第一段代码时,它会发送以下错误消息:


  SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以在'WHERE nume =?附近使用正确的语法。 AND prenume =?'在第1行


您能给我一些如何解决这个问题的提示吗?谢谢!

最佳答案

INSERT语句没有WHERE子句。您需要改用UPDATE

$s = $db->prepare("UPDATE rezultate SET obtinute=:o WHERE nume=:n AND prenume=:p");

09-26 09:34