Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.38 KB | None | 0 0
  1. <?php
  2.  
  3. //DB-varaibles
  4. $dsn = "mysql:dbname=dbirkemalm_com;host=dbirkemalm.com.mysql;";
  5. $username = "dbirkemalm_com";
  6. $password ="8ejxQ4Pn";
  7. $db_name = "dbirkemalm_com";
  8. $vote_table = 'votes';
  9.  
  10. //Variables
  11. $ip = $_SERVER['REMOTE_ADDR'];
  12. $vote = $_GET['id'];
  13.  
  14. //Connect to DB
  15. try {
  16.     $dbh = new PDO($dsn, $username, $password);
  17.     }   catch (PDOException $e) {
  18.     echo 'Connection failed: ' . $e->getMessage();
  19. }
  20.  
  21. $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  22.    
  23. //Insert vote and IP
  24. $ipinsql = "INSERT INTO $vote_table (ip, vote) VALUES(:ip, :vote)";
  25. $ipinsq = $dbh->prepare($ipinsql);
  26. $ipinsq->bindParam(':ip', $ip, PDO::PARAM_STR);
  27. $ipinsq->bindParam(':vote', $vote, PDO::PARAM_STR);
  28. $ipinsq->execute();
  29.  
  30. //Total number of votes
  31.  
  32. $totalsql = "SELECT * FROM $vote_table";
  33. $totalq = $dbh->prepare($totalsql);
  34. $totalq->execute();
  35. $total = $totalq->rowCount();
  36.  
  37.  
  38. //Get number of A-votes
  39. $bar1sql = "SELECT * FROM $vote_table WHERE vote = 'A'";
  40. $bar1q = $dbh->prepare($bar1sql);
  41. $bar1q->execute();
  42. $bar1count = $bar1q->rowCount();
  43.  
  44. //Get number of B-votes
  45. $bar2sql = "SELECT * FROM $vote_table WHERE vote = 'B'";
  46. $bar2q = $dbh->prepare($bar2sql);
  47. $bar2q->execute();
  48. $bar2count = $bar2q->rowCount();
  49.  
  50. //Get number of C-votes
  51. $bar3sql = "SELECT * FROM $vote_table WHERE vote = 'C'";
  52. $bar3q = $dbh->prepare($bar3sql);
  53. $bar3q->execute();
  54. $bar3count = $bar3q->rowCount();
  55.  
  56. //Get number of D-votes
  57. $bar4sql = "SELECT * FROM $vote_table WHERE vote = 'D'";
  58. $bar4q = $dbh->prepare($bar4sql);
  59. $bar4q->execute();
  60. $bar4count = $bar4q->rowCount();
  61.  
  62.  
  63. //Mathematics bar 1
  64. $bar1length = abs($bar1count/$total*100);
  65.  
  66. //Mathematics bar 2
  67. $bar2length = abs($bar2count/$total*100);
  68.  
  69. //Mathematics bar 3
  70. $bar3length = abs($bar3count/$total*100);
  71.  
  72. //Mathematics bar 4
  73. $bar4length = abs($bar4count/$total*100);
  74.  
  75. $totals = json_encode(array("total"=>$total, "bar1"=>$bar1length, "bar2"
  76. =>$bar2length, "bar3"=>$bar3length, "bar4"=>$bar4length));
  77. echo $totals;
  78.  
  79.  
  80. /* Testing count
  81. echo "Total: ",$total, "<br />";
  82. echo "Bar 1: ",$bar1count, "<br />";
  83. echo "Bar 2: ",$bar2count, "<br />";
  84. echo "Bar 3: ",$bar3count, "<br />";
  85. echo "Bar 4: ",$bar4count, "<br />";
  86. */
  87. //print_r($array);
  88. ?>
  89.  
  90. This is my error: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '81.235.240.42' for key 2'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement