simple mysql vote script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ferric
Forum Newbie
Posts: 16
Joined: Wed Mar 17, 2004 3:16 pm

simple mysql vote script

Post 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
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post 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
Post Reply