2015-05-07 197 views
0

我在下面的PHP代碼:MySQL錯誤:1064

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]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE nume=? AND prenume=?' at line 1

你能給我一個關於如何解決這個問題的提示嗎?謝謝!

+1

'INSERT INTO rezultate' did you mean'update rezultate'? –

+0

你的意思是改爲'UPDATE'嗎? – Augwa

+0

是的......應該有更新。尷尬:( –

回答

7

INSERT陳述沒有WHERE子句。您需要改用UPDATE

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