Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. <?PHP
  2.  
  3. include("config.php"); //config.php is added to get access to database connection config values
  4. link = mysql_connect($hostname,$db_username,$db_password);
  5. if (!$link) {
  6. die('Could not connect: ' . mysql_error());
  7. }
  8. @mysql_select_db($database) or die ("unable to select database");
  9.  
  10. function getAllVotes($id){ /**Returns an array whose first element is votes_up and the second one is votes_down **/
  11. $votes = array();
  12. $q = "SELECT * FROM brownies WHERE id = $id";
  13. $r = mysql_query($q);
  14. if(mysql_num_rows($r)==1)//id found in the table {
  15. $row = mysql_fetch_assoc($r);
  16. $votes[0] = $row['voteUp'];
  17. $votes[1] = $row['voteDown'];
  18. }
  19. return $votes;
  20. }
  21.  
  22. $id = $_POST['id'];
  23. $action = $_POST['action'];
  24.  
  25. //get the current votes
  26. $cur_votes = getAllVotes($id);
  27.  
  28. //ok, now update the votes
  29. if($action=='vote_up') //voting up {
  30. $votes_up = $cur_votes[0]+1;
  31. $q = "UPDATE brownies SET voteUp = $votes_up WHERE id = $id";
  32. }
  33. elseif($action=='vote_down') //voting down {
  34. $votes_down = $cur_votes[1]+1;
  35. $q = "UPDATE brownies SET voteDown = $votes_down WHERE id = $id";
  36. }
  37.  
  38. $r = mysql_query($q);
  39. if($r) //voting done {
  40. if($action=='vote_up') //voting up {
  41. echo $votes_up;
  42. }
  43. elseif($action=='vote_down') //voting down {
  44. echo $votes_down;
  45. }
  46. }
  47. elseif(!$r) //voting failed {
  48. echo "Failed!";
  49. }
  50. mysql_close($link);
  51.  
  52. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement