<?php
include ("inc/inc_connect.php");
//check this user hasn't voted (and is using his back page like a naughty person!

$result=mysql_query("SELECT * FROM $votedtable WHERE intVoteId = '$vote' AND strName = '$gfcookie[1]'");
$num_rows = mysql_num_rows( $result );
if ($num_rows > 0)
{
    header("Location: topic.php?topicid=$topicid");
    exit;
}



//set this user as 'voted'

$query = "INSERT INTO $votedtable (strName, intVoteId) values( '$gfcookie[1]', '$vote')";
mysql_query( $query, $link ) or die ( "Error! : ".mysql_error() );

$result=mysql_query("SELECT * FROM $votetable WHERE intId = '$vote' AND intTieToTopic = '$topicid'");
$num_rows = mysql_num_rows( $result ) or die ( "Error2! : ".mysql_error() );;
while ( $a_row = mysql_fetch_object( $result ) )
{
    $votes = array(1=> $a_row->intVotes1, $a_row->intVotes2, $a_row->intVotes3, $a_row->intVotes4, $a_row->intVotes5, $a_row->intVotes6, $a_row->intVotes7, $a_row->intVotes8, $a_row->intVotes9, $a_row->intVotes10, $a_row->intVotes11);
    $votes[$choice] ++;

    $voted = array(1=> $a_row->strVoteFor1, $a_row->strVoteFor2, $a_row->strVoteFor3, $a_row->strVoteFor4, $a_row->strVoteFor5, $a_row->strVoteFor6, $a_row->strVoteFor7, $a_row->strVoteFor8, $a_row->strVoteFor9, $a_row->strVoteFor10, $a_row->strVoteFor11);
    $voted[$choice] = $voted[$choice].",".$gfcookie[1];

    $totalvotes = $a_row->intTotalVotes + 1;
}

$query = "UPDATE $votetable SET intVotes1 = '$votes[1]', intVotes2 = '$votes[2]', intVotes3 = '$votes[3]', intVotes4 = '$votes[4]', intVotes5 = '$votes[5]', intVotes6 = '$votes[6]', intVotes7 = '$votes[7]', intVotes8 = '$votes[8]', intVotes9 = '$votes[9]', intVotes10 = '$votes[10]', intVotes11 = '$votes[11]', strVoteFor1 = '$voted[1]', strVoteFor2 = '$voted[2]', strVoteFor3 = '$voted[3]', strVoteFor4 = '$voted[4]', strVoteFor5 = '$voted[5]', strVoteFor6 = '$voted[6]', strVoteFor7 = '$voted[7]', strVoteFor8 = '$voted[8]', strVoteFor9 = '$voted[9]', strVoteFor10 = '$voted[10]', strVoteFor11 = '$voted[11]', intTotalVotes = '$totalvotes' WHERE intId = '$vote'";
mysql_query( $query, $link ) or die ( "Error! : ".mysql_error() );



header("Location: topic.php?topicid=$topicid");
exit;