Home Backend Development PHP Tutorial How to use PHP to store data after form submission

How to use PHP to store data after form submission

Aug 10, 2023 pm 04:05 PM
data storage php form processing Data submission

How to use PHP to store data after form submission

How to use PHP to implement data storage after form submission

Overview:
In website development, forms are one of the important components for interacting with users. After the user fills out the form and submits it, the submitted data usually needs to be stored for subsequent processing and data analysis. This article will introduce how to use PHP to implement data storage after form submission.

Steps:

  1. Create HTML form:
    First, we need to create an HTML form on the front-end page so that users can fill in relevant information. The following is a simple example:
<form action="submit.php" method="POST">
  <label for="name">姓名:</label>
  <input type="text" name="name" id="name" required><br><br>
  
  <label for="email">邮箱:</label>
  <input type="email" name="email" id="email" required><br><br>
  
  <label for="message">留言:</label>
  <textarea name="message" id="message" required></textarea><br><br>
  
  <input type="submit" value="提交">
</form>
Copy after login
  1. Create PHP file:
    In the above HTML, we point the form submission action to the PHP file named "submit.php" . Therefore, we need to create a file called "submit.php" to handle form submission.
<?php
// 接收表单提交的数据
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

// 建立数据库连接
$servername = "localhost";
$username = "数据库用户名";
$password = "数据库密码";
$dbname = "数据库名";

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

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

// 将数据存储到数据库中
$sql = "INSERT INTO 表名 (name, email, message)
        VALUES ('$name', '$email', '$message')";

if ($conn->query($sql) === TRUE) {
    echo "数据存储成功";
} else {
    echo "数据存储失败: " . $conn->error;
}

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

In the above code, we first obtain the data submitted by the form through the $_POST variable and store it in the corresponding variable. Then, use the mysqli class to establish a connection to the database. Next, we use SQL statements to insert data into the specified database table. If the insertion is successful, a success message is returned; otherwise, a failure message is returned. Finally, the database connection is closed by calling the close() method.

It should be noted that the "database user name", "database password", "database name", and "table name" in the above code need to be modified according to the actual situation.

  1. Create database and tables:
    In order to test the above code, we need to create the database and tables first. You can use the following SQL statement to create a database named "mydatabase" and a table named "messages":
CREATE DATABASE mydatabase;

USE mydatabase;

CREATE TABLE messages (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(30) NOT NULL,
    email VARCHAR(50) NOT NULL,
    message TEXT NOT NULL
);
Copy after login

The above SQL statement creates a table named "messages" , which contains four fields: id, name, email and message. Among them, the id field is an auto-incrementing primary key, and the name, email, and message fields store user name, email, and message information respectively.

Conclusion:
Through the above steps, we successfully implemented the use of PHP to handle data storage after form submission. When the user fills out the form and clicks the submit button, the PHP code receives the form data and stores it in the database. We can further process or analyze the data according to actual needs.

The above is the detailed content of How to use PHP to store data after form submission. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Why can't localstorage successfully save data? Why can't localstorage successfully save data? Jan 03, 2024 pm 01:41 PM

Why does storing data to localstorage always fail? Need specific code examples In front-end development, we often need to store data on the browser side to improve user experience and facilitate subsequent data access. Localstorage is a technology provided by HTML5 for client-side data storage. It provides a simple way to store data and maintain data persistence after the page is refreshed or closed. However, when we use localstorage for data storage, sometimes

How to implement image storage and processing functions of data in MongoDB How to implement image storage and processing functions of data in MongoDB Sep 22, 2023 am 10:30 AM

Overview of how to implement image storage and processing functions of data in MongoDB: In the development of modern data applications, image processing and storage is a common requirement. MongoDB, a popular NoSQL database, provides features and tools that enable developers to implement image storage and processing on its platform. This article will introduce how to implement image storage and processing functions of data in MongoDB, and provide specific code examples. Image storage: In MongoDB, you can use GridFS

How to implement polymorphic storage and multidimensional query of data in MySQL? How to implement polymorphic storage and multidimensional query of data in MySQL? Jul 31, 2023 pm 09:12 PM

How to implement polymorphic storage and multidimensional query of data in MySQL? In practical application development, polymorphic storage and multidimensional query of data are a very common requirement. As a commonly used relational database management system, MySQL provides a variety of ways to implement polymorphic storage and multidimensional queries. This article will introduce the method of using MySQL to implement polymorphic storage and multi-dimensional query of data, and provide corresponding code examples to help readers quickly understand and use it. 1. Polymorphic storage Polymorphic storage refers to the technology of storing different types of data in the same field.

PHP form processing: data presets and default value settings PHP form processing: data presets and default value settings Aug 07, 2023 pm 12:00 PM

PHP form processing: data presets and default value settings When developing web applications, forms are an inevitable part. When a user submits a form, we need to process this data and act accordingly. This article will focus on how to handle presets and default value settings for form data in PHP. Data preset Data preset refers to setting default values ​​for input fields in the form when the form is loaded. In this way, when users fill out the form, they can see that some fields already have default values, which is convenient for users to operate. In PHP, you can use HTML

How to handle autofill and autocomplete in PHP forms How to handle autofill and autocomplete in PHP forms Aug 11, 2023 pm 06:39 PM

How to Handle Autofill and Autocomplete in PHP Forms As the Internet develops, people increasingly rely on autofill and autocomplete features to simplify their operations on the website. Implementing these functions in PHP forms is not complicated. This article will briefly introduce how to use PHP to handle auto-fill and auto-complete of forms. Before we begin, we need to clarify what autofill and autocomplete are. Autofill refers to automatically filling in the fields in a form for users based on their previous input or history. For example, when the user enters an email

How to handle dynamically generated forms using PHP How to handle dynamically generated forms using PHP Aug 13, 2023 pm 01:46 PM

How to handle dynamically generated forms using PHP In web development, forms are one of the most common elements for interacting with users. In some cases, we may need to generate a form dynamically, changing the content and structure of the form according to the user's needs or options. PHP is a powerful back-end programming language that can help us process dynamically generated form data. This article will introduce how to use PHP to handle dynamically generated forms. First, we need to understand how to dynamically generate a form. In HTML, you can use PHP code to embed H

Interaction between Redis and Golang: How to achieve fast data storage and retrieval Interaction between Redis and Golang: How to achieve fast data storage and retrieval Jul 30, 2023 pm 05:18 PM

Interaction between Redis and Golang: How to achieve fast data storage and retrieval Introduction: With the rapid development of the Internet, data storage and retrieval have become important needs in various application fields. In this context, Redis has become an important data storage middleware, and Golang has become the choice of more and more developers because of its efficient performance and simplicity of use. This article will introduce readers to how to interact with Golang through Redis to achieve fast data storage and retrieval. 1.Re

Yii framework middleware: providing multiple data storage support for applications Yii framework middleware: providing multiple data storage support for applications Jul 28, 2023 pm 12:43 PM

Yii framework middleware: providing multiple data storage support for applications Introduction Middleware (middleware) is an important concept in the Yii framework, which provides multiple data storage support for applications. Middleware acts like a filter, inserting custom code between an application's requests and responses. Through middleware, we can process, verify, filter requests, and then pass the processed results to the next middleware or final handler. Middleware in the Yii framework is very easy to use

See all articles