Creating a Simple Polling System using PHP
Polling systems are an essential feature in many websites, enabling users to participate and share their opinions on various topics. In this blog post, we will guide you through creating a simple polling system using PHP.
Prerequisites
To create a polling system using PHP, ensure you have the following prerequisites:
- A text editor or an Integrated Development Environment (IDE) to write and save your PHP files.
- Basic knowledge of HTML and CSS for designing your user interface.
- A web server with PHP installed, such as Apache or Nginx.
Setting Up the Database
First, let's set up a MySQL database to store the polling data. We will create a table named polls
with the following columns:
id
- An integer primary key.question
- A text column for storing the question.option1
,option2
, ...,optionN
- Text columns for storing each option.votes1
,votes2
, ...,votesN
- Integer columns for storing the number of votes for each option.
You can use any MySQL client or PHPMyAdmin to create and modify your database and table.
Creating the Polling Page
Create an index.php
file with the following content:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT question, option1, option2 FROM polls ORDER BY RAND() LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<h2>" . $row["question"] . "</h2>";
echo "<form method='post' action='vote.php'>";
echo "<input type='hidden' name='poll_id' value='" . $row["id"] . "'>";
echo "<input type='radio' id='option1' name='option' value='option1'>";
echo "<label for='option1'>" . $row["option1"] . "</label><br>";
echo "<input type='radio' id='option2' name='option' value='option2'>";
echo "<label for='option2'>" . $row["option2"] . "</label><br>";
echo "<button type='submit'>Vote</button>";
echo "</form>";
}
} else {
echo "No polls available.";
}
$conn->close();
?>
Replace username
, password
, and database_name
with your MySQL credentials.
Creating the Voting Page
Create a new file named vote.php
with the following content:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$poll_id = $_POST['poll_id'];
$option = $_POST['option'];
$sql = "UPDATE polls SET votes1 = votes1 + 1, votes2 = CASE option = 'option1' THEN votes2 + 1 ELSE votes2 END WHERE id = $poll_id";
if ($conn->query($sql) === TRUE) {
echo "Thank you for your vote!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Replace username
, password
, and database_name
with your MySQL credentials.
Now, you have a simple polling system using PHP. To test it, save the files in the same directory on your web server, and visit the index.php
page in your web browser. Participate in the poll, and the results will be updated automatically.