Home > Backend Development > PHP Tutorial > How to use PHP to develop a simple online submission function

How to use PHP to develop a simple online submission function

王林
Release: 2023-09-22 08:06:01
Original
1395 people have browsed it

How to use PHP to develop a simple online submission function

How to use PHP to develop a simple online submission function

In the Internet era, people have an increasing demand for channels to obtain information, and the online submission function has become a website One of the important functions. For some communities, blogs or news websites, providing the function for users to contribute online can not only enrich the content of the website, but also attract more users to participate and interact. This article will introduce how to use PHP to develop a simple online submission function to help websites quickly implement this function.

1. Preparation

Before starting to write specific code, we need to prepare the following work:

  1. Server environment: Make sure that the PHP running environment has been installed on the server .
  2. Database preparation: Prepare a data table named "articles" to save relevant information about submissions. The table can contain the following fields: id (article ID, auto-incremented primary key), title (article title), content (article content), author (author), submitTime (submission time) and other fields.
  3. Create a database connection: Create a connection to the database in the PHP code for subsequent operations on the database.

2. Page construction

  1. Submission page

Create a file named "submit_article.php" to implement the user's Contribute.

First, we need to add a form to the page for users to enter relevant information for submission. The code is as follows:

<form action="save_article.php" method="post">
    <label>文章标题:</label>
    <input type="text" name="title" required><br><br>
    
    <label>文章内容:</label><br>
    <textarea name="content" rows="10" cols="30" required></textarea><br><br>
    
    <label>作者:</label>
    <input type="text" name="author" required><br><br>
    
    <input type="submit" value="提交">
</form>
Copy after login

In the above code, we use the HTML form tag to create a form, and adds corresponding labels and name attributes to each input item. Among them, in order to ensure that users must fill in relevant information, we use the required attribute of HTML5.

  1. Submission success page

Create a file named "submit_success.php" to display the page where the user has successfully submitted their submission.

In this page, we can simply display the relevant information of the submission. The code is as follows:

<h3>投稿成功!</h3>
<p>文章标题:<?php echo $_POST['title']; ?></p>
<p>文章内容:<?php echo $_POST['content']; ?></p>
<p>作者:<?php echo $_POST['author']; ?></p>
Copy after login

In the above code, we use the PHP echo statement to output the submission information submitted by the user. onto the page.

3. Data processing

  1. Database connection

Before we start processing data, we need to create a file named "db.php". Used to create a connection to the database. The code is as follows:

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

    // 创建与数据库的连接
    $conn = new mysqli($servername, $username, $password, $dbname);

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

Please replace $servername, $username, $password, $dbname with actual database connection information.

  1. Save submission data

In the page "save_article.php", we need to receive the submission information submitted by the user and save it to the database. The code is as follows:

<?php
    include "db.php"; // 包含数据库连接文件

    $title = $_POST["title"];
    $content = $_POST["content"];
    $author = $_POST["author"];
    $submitTime = date("Y-m-d H:i:s"); // 获取当前时间

    // 构建SQL语句
    $sql = "INSERT INTO articles (title, content, author, submitTime)
            VALUES ('$title', '$content', '$author', '$submitTime')";

    if ($conn->query($sql) === TRUE) {
        // 保存成功,跳转到投稿成功页面
        header("Location: submit_success.php");
        exit();
    } else {
        echo "保存失败:" . $conn->error;
    }

    $conn->close(); // 关闭数据库连接
?>
Copy after login

In the above code, we first include the database connection file "db.php", and then obtain the contribution information submitted by the user through the $_POST super global variable. Next, we use the INSERT INTO statement to insert information into the database. If the save is successful, jump to the "submit_success.php" page, otherwise an error message will be output on the current page.

4. Effect Demonstration

So far, we have completed the development of the online submission function. Users can enter relevant information on the submission page and click the submit button to submit. After the submission is successful, it will jump to the submission success page and display the relevant information submitted by the user.

Through the study of this article, we learned how to use PHP to develop a simple online submission function. Hopefully this article will help you add this important feature to your website faster. At the same time, in order to achieve a more complete submission function, you can further improve it, such as adding a submission list, submission details page, etc.

The above is the detailed content of How to use PHP to develop a simple online submission function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template