Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- include("config.php"); //config.php is added to get access to database connection config values
- link = mysql_connect($hostname,$db_username,$db_password);
- if (!$link) {
- die('Could not connect: ' . mysql_error());
- }
- @mysql_select_db($database) or die ("unable to select database");
- function getAllVotes($id){ /**Returns an array whose first element is votes_up and the second one is votes_down **/
- $votes = array();
- $q = "SELECT * FROM brownies WHERE id = $id";
- $r = mysql_query($q);
- if(mysql_num_rows($r)==1)//id found in the table {
- $row = mysql_fetch_assoc($r);
- $votes[0] = $row['voteUp'];
- $votes[1] = $row['voteDown'];
- }
- return $votes;
- }
- $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 brownies SET voteUp = $votes_up WHERE id = $id";
- }
- elseif($action=='vote_down') //voting down {
- $votes_down = $cur_votes[1]+1;
- $q = "UPDATE brownies SET voteDown = $votes_down WHERE id = $id";
- }
- $r = mysql_query($q);
- if($r) //voting done {
- if($action=='vote_up') //voting up {
- echo $votes_up;
- }
- elseif($action=='vote_down') //voting down {
- echo $votes_down;
- }
- }
- elseif(!$r) //voting failed {
- echo "Failed!";
- }
- mysql_close($link);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement