Home >Backend Development >PHP Tutorial >How to use PHP to develop simple questionnaire surveys and statistical functions

How to use PHP to develop simple questionnaire surveys and statistical functions

WBOY
WBOYOriginal
2023-09-21 13:31:411425browse

How to use PHP to develop simple questionnaire surveys and statistical functions

How to use PHP to develop simple questionnaire surveys and statistical functions

In recent years, with the popularity and development of the Internet, questionnaire surveys have become a common data collection Way. As a common server-side programming language, PHP is easy to learn and use, and is widely used to develop Web applications. This article will introduce how to use PHP to develop a simple questionnaire survey and statistical functions, and provide specific code examples.

1. Database design
First of all, we need to design a suitable database to store the relevant data of the questionnaire survey. Assume that our questionnaire survey has the following fields:

  1. id: questionnaire ID, primary key, auto-increment;
  2. title: questionnaire title, string;
  3. content: Questionnaire content, long text;
  4. submit_time: submission time, timestamp;
  5. ip_address: submitter IP address, string.

We can use the MySQL database to store this data and create a table named "survey" to save the questionnaire data. The specific SQL statements are as follows:

CREATE TABLE survey (

id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255),
content TEXT,
submit_time INT,
ip_address VARCHAR(50)

);

2. Write the questionnaire page
Next, we need to write a page for display A page that takes surveys and receives answers submitted by users. The specific code is as follows:

<!DOCTYPE html>
<html>
<head>
    <title>问卷调查</title>
</head>
<body>
    <h1>问卷调查</h1>
    <form action="submit.php" method="post">
        <label for="answer">请回答以下问题:</label><br>
        <input type="text" name="answer" id="answer"><br><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

3. Write the data submission processing page
After the user submits the answer, we need to store the data in the database. To do this, we need to write a page that handles data submission. The specific code is as follows:

<?php
// 提交数据到数据库
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $answer = $_POST['answer'];
    $timestamp = time();
    $ipAddress = $_SERVER['REMOTE_ADDR'];

    $conn = mysqli_connect('localhost', 'username', 'password', 'database');
    mysqli_set_charset($conn, 'utf8');

    $sql = "INSERT INTO survey (title, content, submit_time, ip_address) VALUES ('', '$answer', '$timestamp', '$ipAddress')";
    mysqli_query($conn, $sql);
    mysqli_close($conn);

    echo "提交成功!";
}

Please note that the database connection parameters in the above code need to be modified according to the actual situation.

4. Write a data statistics page
Finally, we need to write a page to display the statistical results of the questionnaire. The specific code is as follows:

<?php
// 查询数据库中的数据并统计数量
$conn = mysqli_connect('localhost', 'username', 'password', 'database');
mysqli_set_charset($conn, 'utf8');

$sql = "SELECT COUNT(*) AS count FROM survey";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$count = $row['count'];

mysqli_close($conn);
?>

<!DOCTYPE html>
<html>
<head>
    <title>问卷统计</title>
</head>
<body>
    <h1>问卷统计</h1>
    <p>已有 <?php echo $count; ?> 人参与了问卷调查。</p>
</body>
</html>

Please ensure that the database connection parameters in the above code are the same as before.

Summary: This article introduces how to use PHP to develop a simple questionnaire survey and statistical functions. By writing questionnaire pages, data submission processing pages and data statistics pages, we can implement simple questionnaire surveys and statistical functions. We hope that readers can further improve and expand the functions based on the code examples provided in this article to meet actual needs. Please adjust the specific code and database design according to the actual situation.

The above is the detailed content of How to use PHP to develop simple questionnaire surveys and statistical functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn