Page 1 of 1

simple mysql vote script

Posted: Sun Mar 28, 2004 3:45 pm
by ferric
Here's the code:

Code: Select all

<?php

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to select database");


$query = "SELECT * FROM video WHERE id='$id'";
$result = mysql_query($query);

while ($r = mysql_fetch_array($result)) {

        $vote = $_GET['vote'];
	$votetotal = $r["votetotal"];
	$totalvotes = $r["totalvotes"];

	$insvotetotal = $votetotal + $vote;
	$instotalvotes = $totalvotes + 1;

	mysql_query("UPDATE video SET (votetotal='$insvotetotal', totalvotes='$instotalvotes') 	WHERE id='$id'");
	mysql_close();

	echo "Thank you for your vote!  Current vote: $votetotal/$totalvotes <B>";
}
?>
Not sure what i'm doing wrong, but the votetotal and totalvotes fields won't move from 0 in my database.

FYI, votetotal is the accumulated total of all votes. totalvotes is the total number of votes.

thanks

Posted: Sun Mar 28, 2004 8:51 pm
by Deemo
I cant see it at first glance, but something you can do is debug the code
for example, in the line:

Code: Select all

mysql_query("UPDATE video SET (votetotal='$insvotetotal', totalvotes='$instotalvotes')    WHERE id='$id'");
Instead change it to the same format as your first query, then echo that query to make sure that the UPDATE statement is correct

Click Here for MySQL Debugging Tips