diff --git a/README.md b/README.md index 6cf263c..c0934fb 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Basic steps include: 3. Create a new table in the database named 'poll_votes" with the following structure: CREATE TABLE poll_votes (option_id INTEGER); + ALTER TABLE poll_votes ADD COLUMN ip_address TEXT; 4. Create a text file named poll_options.txt and add the expiry date of the poll and the poll options to the file. The expiry date should be the first line of the file, followed by the poll options. Each poll option should be on a new line. diff --git a/index.php b/index.php index 0f7bd9a..ed12ee9 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,7 @@ - + success) { // reCaptcha was successful + // Check if the user has already voted + $stmt = $db->prepare("SELECT COUNT(*) FROM poll_votes WHERE ip_address = ?"); + $stmt->execute([$_SERVER['REMOTE_ADDR']]); + $count = $stmt->fetchColumn(); + if ($count > 0) { + // User has already voted + echo "

You have already voted in this poll.

"; + + exit; + } // Save the vote to the database - $stmt = $db->prepare("INSERT INTO poll_votes (option_id) VALUES (?)"); - $stmt->execute([$_POST['option']]); + $stmt = $db->prepare("INSERT INTO poll_votes (option_id, ip_address) VALUES (?, ?)"); + $stmt->execute([$_POST['option'], $_SERVER['REMOTE_ADDR']]); // Redirect the user to a different page header('Location: results.php'); @@ -59,7 +70,7 @@ if ($result->success) { } else { // reCaptcha was unsuccessful // Display an error message - echo "

There was an error with the reCaptcha. Please try again.

"; + echo "

There was an error with the reCaptcha. Please try again.

"; } } @@ -76,6 +87,8 @@ echo ""; echo ""; // Display the "View Results" link -echo "View Results"; +// echo "View Results"; ?> + +
diff --git a/poll.db b/poll.db index fd2cdf0..cc017ac 100644 Binary files a/poll.db and b/poll.db differ diff --git a/poll_options.txt b/poll_options.txt index 39379e3..68b537d 100644 --- a/poll_options.txt +++ b/poll_options.txt @@ -1,5 +1,4 @@ -2023-02-01 -Test -This is a song -this is my mother -dude where is my car +2023-01-05 +Yes +No +Depends how major diff --git a/question.txt b/question.txt index 1f23bf9..02a7591 100644 --- a/question.txt +++ b/question.txt @@ -1 +1 @@ -Who am I really? +Changes are possibly on the horizon. Are you open to big changes to tilde.club? diff --git a/results.php b/results.php index fb5a59c..09594b3 100644 --- a/results.php +++ b/results.php @@ -10,16 +10,39 @@ array_shift($options); // Get the total number of votes $total_votes = $db->query("SELECT COUNT(*) FROM poll_votes")->fetchColumn(); -// Display the results +// Prepare the data for the chart +$data = [['Option', 'Votes']]; foreach ($options as $id => $option) { // Get the vote count for this option $vote_count = $db->query("SELECT COUNT(*) FROM poll_votes WHERE option_id=$id")->fetchColumn(); - // Calculate the percentage of votes for this option - $percentage = round(($vote_count / $total_votes) * 100); - - // Display the results - echo "$option: $percentage% ($vote_count votes)
"; + // Add the data for this option to the chart data + $data[] = [$option, $vote_count]; } ?> + + + + + + +
+

+ + + diff --git a/styles.css b/styles.css new file mode 100644 index 0000000..ae5c7b7 --- /dev/null +++ b/styles.css @@ -0,0 +1,4 @@ +/* Orange color */ +.orange { + color: #ffa500; +}