How do I use PHP to create an online poll or survey?

Creating an Online Poll or Survey using PHP

In this tutorial, we’ll walk through the process of creating a simple online poll or survey using PHP, MySQL, and HTML. We’ll cover creating the database, designing the poll form, handling user submissions, and displaying the results.

Step 1: Setting up the database

First, let’s create a MySQL database to store our poll data. We’ll create a table called ‘polls’ to store the poll questions and a table called ‘votes’ to store the user responses.

-- SQL
CREATE DATABASE poll_db;

USE poll_db;

CREATE TABLE polls (
  id INT AUTO_INCREMENT PRIMARY KEY,
  question TEXT NOT NULL
);

CREATE TABLE votes (
  id INT AUTO_INCREMENT PRIMARY KEY,
  poll_id INT NOT NULL,
  option_text VARCHAR(255) NOT NULL,
  votes INT DEFAULT 0,
  FOREIGN KEY (poll_id) REFERENCES polls(id)
);

Step 2: Create a PHP file to connect to the database

Next, we’ll create a PHP file named ‘db_connect.php’ to establish a connection with the database.

<?php
# PHP
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "poll_db";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
?>

Step 3: Designing the poll form

Now, let’s create an HTML form where users can submit their votes. We’ll name this file ‘index.php’.

<!-- html -->
<!DOCTYPE html>
<html>
<head>
  <title>Simple Poll</title>
</head>
<body>
  <h1>Simple Poll</h1>
  <form action="submit_vote.php" method="POST">
    <label for="poll">What is your favorite color?</label>
    <select name="poll" id="poll">
      <option value="1">Red</option>
      <option value="2">Blue</option>
      <option value="3">Green</option>
    </select>
    <input type="submit" value="Submit">
  </form>
</body>
</html>

Step 4: Handling user submissions

Next, let’s create a PHP file named ‘submit_vote.php’ to process user submissions and store them in the database.

<?php
# PHP
require 'db_connect.php';

$poll_option = $_POST['poll'];

$sql = "UPDATE votes SET votes = votes + 1 WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $poll_option);

if ($stmt->execute()) {
  header("Location: results.php");
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}

$stmt->close();
$conn->close();
?>

Step 5: Displaying poll results

Finally, let’s create a PHP file named ‘results.php’ to display the poll results.

<?php
# PHP
require 'db_connect.php';

$sql = "SELECT option_text, votes FROM votes ORDER BY id";
$result = $conn->query($sql);
?>
<!-- html -->
<!DOCTYPE html>
<html>
<head>
  <title>Poll Results</title>
</head>
<body>
  <h1>Poll Results</h1>
  <table>
    <tr>
      <th>Option</th>
      <th>Votes</th>
    </tr>
    <?php
    # PHP
    if($result->num_rows > 0) {
  while ($row = $result->fetch_assoc()) {
    echo "<tr><td>" . $row["option_text"] . "</td><td>" . $row["votes"] . "</td></tr>";
  }
} else {
  echo "<tr><td colspan='2'>No votes found</td></tr>";
}
?>
</table>
  <a href="index.php">Vote again</a>
</body>
</html>
<?php
# PHP
$conn->close();
?>

Conclusion:

In this tutorial, we created a simple online poll using PHP, MySQL, and HTML. We designed a poll form, processed user submissions, and displayed the poll results. To create more advanced polls or surveys, you can add additional features such as user authentication, input validation, or multiple question types.

Got question?

Submit it here

© All rights reserved.