Coooooooookies!

Hi guys, im trying to create a voting system where you can vote for games and stuff… Your vote is stored on my DB, but you will also receive a cookie so you cant revote…

<?

if(isset($_GET["v"])) {
$myvote = $_GET["v"];
$cookiename = $_GET["slug"];
$gameid = $_GET["id"];
$newvalue = $_COOKIE["vgratgam"].",".$cookiename;
$mycookie = $_COOKIE["vgratgam"];
$cookiearray = explode(",", $mycookie);


if (isset($_COOKIE["vgratgam"])) { 
//Cookie exist, check if you already voted for this game.

foreach ($cookiearray as $regvot) {
if($regvot == $cookiename) {
$timesvoted = $timesvoted + 1;
} else { 
$timesvoted = $timesvoted + 0;
}

if($timesvoted == '0') {
//user already voted on this submission!
header("Location: ../play.php?id=$gameid&e");
} else {
setcookie(vgratgam, $newvalue, time()+86400); // Update the cookie!
header("Location: ../play.php?id=$gameid&s");


require_once("DbConnector.php"); 
$db = new DbConnector();
$db->connect();

$query = "SELECT * FROM games WHERE id='$gameid'";
$result = $db->query($query);
$rows = $db->fetchArray($result); // Get the profile from database

$votes = $rows['votes'];
$score = $rows['score'];

$votes = $votes + 1;
$score = $score + $myvote;
$totscore = $score / $votes;


mysql_query("UPDATE games SET score='$score', votes='$votes', totalscore='$totscore' WHERE id='$gameid'");
} }
} else { // the cookie expired or it was never created 
header("Location: ../play.php?id=$gameid&s");
setcookie(vgratgam, $cookiename, time()+86400); // so make one 

require_once("DbConnector.php"); 
$db = new DbConnector();
$db->connect();

$query = "SELECT * FROM games WHERE id='$gameid'";
$result = $db->query($query);
$rows = $db->fetchArray($result); // Get the profile from database

$votes = $rows['votes'];
$score = $rows['score'];

$votes = $votes + 1;
$score = $score + $myvote;
$totscore = $score / $votes;


mysql_query("UPDATE games SET score='$score', votes='$votes', totalscore='$totscore' WHERE id='$gameid'");
}



}
?>

So what this does, is saving your vote to a DB, and saves a cookie, I want to get ALL the games you vote on in ONE cookie, not thousands of them.

Then if you dont have that cookie it creates a new one, if you have it, it uses explode function to see if you voted at this particular game. And if you havent, it saves your vote and updates it.

What I get so far, is that I always get sucsessfull vote, althrough the cookie gets updated, just that the same game gets listed a couple of times:S

What am I doing wrong:S?