Created
January 23, 2019 18:27
-
-
Save gmelodie/0abaaf3413b2b10b64f68966339de127 to your computer and use it in GitHub Desktop.
Vote page with database version 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<!-- SAME OLD CRAP --> | |
<?php | |
$servername = "localhost"; | |
$username = "gabriel"; | |
$password = "my_secret_password"; | |
$dbname = "herschel"; | |
// Create connection | |
$conn = new mysqli($servername, $username, $password, $dbname); | |
// Check connection | |
if ($conn->connect_error) { | |
die("Connection failed: " . $conn->connect_error); | |
} | |
// Insert data to database | |
$insert = "INSERT INTO vote (id, liked) VALUES ('" . $_SERVER['REMOTE_ADDR'] . "', " . $_POST["liked"] . ")"; | |
// Check if query was successful | |
if ($conn->query($insert) === TRUE) { | |
echo "New record created successfully"; | |
} else { | |
echo "Error: " . $conn->error . "<br>" ; | |
} | |
// Look up likes and dislikes on database | |
$q_yes ="SELECT COUNT(liked) FROM vote WHERE liked=TRUE GROUP BY liked;"; | |
$q_no ="SELECT COUNT(liked) FROM vote WHERE liked=FALSE GROUP BY liked;"; | |
$yes_result = $conn->query($q_yes); | |
$no_result = $conn->query($q_no); | |
$yes = $yes_result->fetch_assoc()["COUNT(liked)"]; | |
$no = $no_result->fetch_assoc()["COUNT(liked)"]; | |
echo "There are " . $yes . " people who liked the page<br>" ; | |
echo "There are " . $no . " people who disliked the page<br>" ; | |
?> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment