0

I'm trying to add vote up / vote down functions for each item I have in a table. I am currently working on implementing this one here: www.technabled.com/2009/02/reddit-style-voting-with-php-mysql-and.html

But, it doesn't work for me. Once action is run, it always returns "FAILED!", and I can't figure out what I'm doing wrong anymore.

This is what's inside my votes.php page.

function getAllVotes($id)
    {
    $votes = array();
    $q = "SELECT * FROM cover WHERE id='$id' ";
    $r = mysql_query($q) or die("Error: ". mysql_error(). " with query ". $q);
    if(mysql_num_rows($r)==1) {

        $row = mysql_fetch_assoc($r);
        $votes[0] = $row['votes_up'];
        $votes[1] = $row['votes_down'];
        }
    return $votes;
    }

function getEffectiveVotes($id)
    {
    /**
    Returns an integer
    **/
    $votes = getAllVotes($id);
    $effectiveVote = $votes[0] - $votes[1];
    return $effectiveVote;
    }

$id = $_POST['id'];
$action = $_POST['action'];

//get the current votes
$cur_votes = getAllVotes($id);

//ok, now update the votes

if($action=='vote_up') //voting up
{
    $votes_up = $cur_votes[0]+1;
    $q = "UPDATE cover SET votes_up = $votes_up WHERE id = $id";
}
elseif($action=='vote_down') //voting down
{
    $votes_down = $cur_votes[1]+1;
    $q = "UPDATE cover SET votes_down = $votes_down WHERE id = $id";
}

$r = mysql_query($q);
if($r) //voting done
    {
    $effectiveVote = getEffectiveVotes($id);
    echo $effectiveVote." votes";
    }
elseif(!$r) //voting failed
    {
    echo "Failed!";
    }

And if it matters, here's the table structure for the columns used:

votes_up and votes_down:
Type: int(11) / Null: No / Default: 0

column id:
Type: int(8) / Null: No / Extra: AUTO_INCREMENT

And I do my checking using parameters:
site.com/votes.php?action=vote_up&id=123

Hopefully someone could spot the error. Thank you for your time and assistance!

Mafia
  • 792
  • 2
  • 19
  • 39
  • 1
    How about `echo 'Failed! ', mysql_error();`? This will at least tell you what was wrong with the query – Phil May 22 '12 at 03:08
  • Thanks @Phil! Here's the error I got: `"Failed! Query was empty"` =/ The id I used exists.. I checked using these parameters: `/votes.php?action=vote_up&id=123` (ID exists) – Mafia May 22 '12 at 03:14
  • 1
    Ah, simple logic error then. `$_POST['action']` is neither *vote_up* or *vote_down* ergo `$q` is undefined when you attempt to use it. This is most probably due to your query vars not being in `$_POST` (ie, you have issued a GET request, not POST) – Phil May 22 '12 at 03:16
  • Hmmm.. I reread your comment & I still don't get it, should I make all $_POST[''] into $_GET then? inside the votes.php page? – Mafia May 22 '12 at 03:19
  • 1
    No, you would need to use `$_GET['action']` in the above case – Phil May 22 '12 at 03:20
  • Gotcha! Now it's returning this error: `Failed! 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 '' at line 1` – Mafia May 22 '12 at 03:21
  • @Phil! I got it to work! :D Changed `$_POST['id'] to $_GET['id']` too and it worked! Thanks so much for the help, I never would have gotten that AT ALL for years. – Mafia May 22 '12 at 03:23
  • Please stop writing new code with the ancient mysql_* functions. They are no longer maintained and community has begun the [deprecation process](http://news.php.net/php.internals/53799). Instead you should learn about prepared statements and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you care to learn, [here is a quite good PDO-related tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – DCoder May 22 '12 at 05:06

0 Answers0