Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple PHP Voting System

Tags:

php

mysql

I have a simple application which allows users to submit 'problems' and then comment on them. I am attempting to make a simple voting system so users can 'vote up' problems which in turn will push them higher up a list. I have some basic knowledge of PHP and everything so far is working, I just can't figure out how to get this to work.

I've followed a tutorial online and so far have this on my problem.php page...

if (isset($_GET['vote'], $_GET['id'])){
        add_problem_vote($_GET['id]'], $_GET['vote']);
    }

<a href="?vote=up&amp;id=<?php echo $problemID; ?>">Vote</a>

And on my functions.php page...

function add_problem_vote($problemID, $vote){

    $problemID = (int)$problemID;

    $vote = ($vote === 'up') ? '+' : '-';

    $sql = "UPDATE `problems` SET `votes` = `votes` {$vote} 1 WHERE `id` = {$problem_id}";

    mysql_query($sql);
}

All my table fields are definitely named correctly. I know there are many things to consider like re-voting after the session has closed but as long as I have shown the idea it doesn't have to be perfect. At the minute when the link is clicked it redirects to a page but the votes don't change in the mysql table.

like image 336
GuerillaRadio Avatar asked May 07 '12 10:05

GuerillaRadio


1 Answers

$sql = "UPDATE `problems` SET `votes` = `votes` ".$vote." 1 WHERE `id` = ".$problem_id;
mysql_query($sql) or die(mysql_error());

Check what error you are getting ?

like image 55
Miqdad Ali Avatar answered Oct 01 '22 14:10

Miqdad Ali