Home  >  Article  >  Backend Development  >  How to use PHP to develop a simple online submission system

How to use PHP to develop a simple online submission system

WBOY
WBOYOriginal
2023-09-24 11:27:16818browse

How to use PHP to develop a simple online submission system

How to use PHP to develop a simple online submission system

Introduction:
With the popularity and development of the Internet, more and more people are willing to use online Share your creations by submitting articles to get wider reading and feedback. This article will introduce how to use PHP to develop a simple online submission system to facilitate users to submit articles and manage submission content.

1. Design the database structure:
Before we begin, we need to design the database structure to store data related to submissions. Here, we can create a table named "articles". The structure contains the following fields:

  • id: The unique identifier of the article, using an auto-incrementing primary key.
  • title: The title of the article, using VARCHAR type, limiting the character length.
  • content: The content of the article, using the TEXT type, used to store large text data.
  • author: The author of the article, using VARCHAR type.
  • submit_date: The submission date of the article, using the DATETIME type.

2. Create a database connection:
In the PHP code, we need to first create a connection to the database. You can create a database connection through the following code:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "article_system";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检测连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
?>

3. Create a submission page:
Next, we need to create a submission page so that users can fill in the article title and content and submit their submission. You can use the following code to create the HTML code of the submission page:

<!DOCTYPE html>
<html>
<head>
    <title>在线投稿系统</title>
</head>
<body>
    <h1>欢迎使用在线投稿系统</h1>
    <form action="submit_article.php" method="POST">
        <label for="title">标题:</label>
        <input type="text" name="title" required><br>

        <label for="content">内容:</label><br>
        <textarea name="content" rows="10" cols="50" required></textarea><br>

        <input type="submit" value="提交">
    </form>
</body>
</html>

4. Processing form submission:
In the submission page, we need to submit the title and content filled in by the user to the server for processing, and store the data Save into database. In order to implement this function, we can create a file named "submit_article.php" and write the following code in it:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "article_system";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检测连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

// 处理表单提交
$title = $_POST['title'];
$content = $_POST['content'];
$author = "匿名作者";
$submit_date = date("Y-m-d H:i:s");

$sql = "INSERT INTO articles (title, content, author, submit_date) VALUES ('$title', '$content', '$author', '$submit_date')";

if ($conn->query($sql) === TRUE) {
    echo "投稿成功!";
} else {
    echo "投稿失败:" . $conn->error;
}

$conn->close();
?>

5. Display the submission list:
In order to facilitate users to view the submitted contributions For articles, we can create a submission list page to display the submission content stored in the database. You can use the following code to create a submission list page:

<!DOCTYPE html>
<html>
<head>
    <title>投稿列表</title>
</head>
<body>
    <h1>投稿列表</h1>
    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "article_system";

    // 创建连接
    $conn = new mysqli($servername, $username, $password, $dbname);

    // 检测连接是否成功
    if ($conn->connect_error) {
        die("连接失败: " . $conn->connect_error);
    }

    // 查询数据库数据
    $sql = "SELECT * FROM articles ORDER BY submit_date DESC";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            echo "<h2>" . $row["title"] . "</h2>";
            echo "<p>" . $row["content"] . "</p>";
            echo "<p>作者:" . $row["author"] . "</p>";
            echo "<p>提交日期:" . $row["submit_date"] . "</p>";
            echo "<hr>";
        }
    } else {
        echo "暂无投稿";
    }

    $conn->close();
    ?>
</body>
</html>

6. Summary:
Through the above steps, we have successfully developed a simple online submission system using PHP. Users can fill in the title and content of the article on the submission page. After clicking the submit button, the relevant information of the article will be saved into the database. In addition, we have also created a submission list page to display the list of submitted articles. This system can facilitate users to submit contributions, and manage and display the submission content.

Summarizing the above steps, we can see that it is not difficult to develop a simple online submission system using PHP. By designing the database structure, creating database connections, writing form processing scripts and displaying list pages, we can implement a basic submission system. Of course, depending on the needs, we can further add functions, such as user registration and login, comment functions, etc., to improve the user experience and the practicality of the system. I hope this article can be helpful to you, and I wish you successful development!

The above is the detailed content of How to use PHP to develop a simple online submission system. 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