Reputation: 1736
I'm having the following PHP code:
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();
}
And the following class:
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();
}
}
}
But when I execute the first piece of code, it sends this error message:
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
Can you give me a tip on how to solve this problem? Thanks!
Upvotes: 0
Views: 106
Reputation: 219924
INSERT
statements do not have a WHERE
clause. You need to use UPDATE
instead.
$s = $db->prepare("UPDATE rezultate SET obtinute=:o WHERE nume=:n AND prenume=:p");
Upvotes: 7