Guest User

Untitled

a guest
Jan 24th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. <html>
  2.  
  3. <!-- SAME OLD CRAP -->
  4.  
  5. <?php
  6. $servername = "localhost";
  7. $username = "gabriel";
  8. $password = "my_secret_password";
  9. $dbname = "herschel";
  10.  
  11. // Create connection
  12. $conn = new mysqli($servername, $username, $password, $dbname);
  13.  
  14. // Check connection
  15. if ($conn->connect_error) {
  16. die("Connection failed: " . $conn->connect_error);
  17. }
  18.  
  19. // Insert data to database
  20. $insert = "INSERT INTO vote (id, liked) VALUES ('" . $_SERVER['REMOTE_ADDR'] . "', " . $_POST["liked"] . ")";
  21.  
  22. // Check if query was successful
  23. if ($conn->query($insert) === TRUE) {
  24. echo "New record created successfully";
  25. } else {
  26. echo "Error: " . $conn->error . "<br>" ;
  27. }
  28.  
  29. // Look up likes and dislikes on database
  30. $q_yes ="SELECT COUNT(liked) FROM vote WHERE liked=TRUE GROUP BY liked;";
  31. $q_no ="SELECT COUNT(liked) FROM vote WHERE liked=FALSE GROUP BY liked;";
  32.  
  33. $yes_result = $conn->query($q_yes);
  34. $no_result = $conn->query($q_no);
  35.  
  36. $yes = $yes_result->fetch_assoc()["COUNT(liked)"];
  37. $no = $no_result->fetch_assoc()["COUNT(liked)"];
  38.  
  39. echo "There are " . $yes . " people who liked the page<br>" ;
  40. echo "There are " . $no . " people who disliked the page<br>" ;
  41. ?>
  42.  
  43. </html>
Add Comment
Please, Sign In to add comment