Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //DB-varaibles
- $dsn = "mysql:dbname=dbirkemalm_com;host=dbirkemalm.com.mysql;";
- $username = "dbirkemalm_com";
- $password ="8ejxQ4Pn";
- $db_name = "dbirkemalm_com";
- $vote_table = 'votes';
- //Variables
- $ip = $_SERVER['REMOTE_ADDR'];
- $vote = $_GET['id'];
- //Connect to DB
- try {
- $dbh = new PDO($dsn, $username, $password);
- } catch (PDOException $e) {
- echo 'Connection failed: ' . $e->getMessage();
- }
- $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
- //Insert vote and IP
- $ipinsql = "INSERT INTO $vote_table (ip, vote) VALUES(:ip, :vote)";
- $ipinsq = $dbh->prepare($ipinsql);
- $ipinsq->bindParam(':ip', $ip, PDO::PARAM_STR);
- $ipinsq->bindParam(':vote', $vote, PDO::PARAM_STR);
- $ipinsq->execute();
- //Total number of votes
- $totalsql = "SELECT * FROM $vote_table";
- $totalq = $dbh->prepare($totalsql);
- $totalq->execute();
- $total = $totalq->rowCount();
- //Get number of A-votes
- $bar1sql = "SELECT * FROM $vote_table WHERE vote = 'A'";
- $bar1q = $dbh->prepare($bar1sql);
- $bar1q->execute();
- $bar1count = $bar1q->rowCount();
- //Get number of B-votes
- $bar2sql = "SELECT * FROM $vote_table WHERE vote = 'B'";
- $bar2q = $dbh->prepare($bar2sql);
- $bar2q->execute();
- $bar2count = $bar2q->rowCount();
- //Get number of C-votes
- $bar3sql = "SELECT * FROM $vote_table WHERE vote = 'C'";
- $bar3q = $dbh->prepare($bar3sql);
- $bar3q->execute();
- $bar3count = $bar3q->rowCount();
- //Get number of D-votes
- $bar4sql = "SELECT * FROM $vote_table WHERE vote = 'D'";
- $bar4q = $dbh->prepare($bar4sql);
- $bar4q->execute();
- $bar4count = $bar4q->rowCount();
- //Mathematics bar 1
- $bar1length = abs($bar1count/$total*100);
- //Mathematics bar 2
- $bar2length = abs($bar2count/$total*100);
- //Mathematics bar 3
- $bar3length = abs($bar3count/$total*100);
- //Mathematics bar 4
- $bar4length = abs($bar4count/$total*100);
- $totals = json_encode(array("total"=>$total, "bar1"=>$bar1length, "bar2"
- =>$bar2length, "bar3"=>$bar3length, "bar4"=>$bar4length));
- echo $totals;
- /* Testing count
- echo "Total: ",$total, "<br />";
- echo "Bar 1: ",$bar1count, "<br />";
- echo "Bar 2: ",$bar2count, "<br />";
- echo "Bar 3: ",$bar3count, "<br />";
- echo "Bar 4: ",$bar4count, "<br />";
- */
- //print_r($array);
- ?>
- 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