Home > Backend Development > PHP Tutorial > PHP form processing: form data insertion and deletion

PHP form processing: form data insertion and deletion

PHPz
Release: 2023-08-07 17:54:02
Original
968 people have browsed it

PHP form processing: form data insertion and deletion

Introduction:
In web applications, forms are a common way of user interaction, through which users can enter data and submit it to The server handles it. The server side is responsible for processing these form data, including inserting and deleting data from the database. This article will introduce how to use PHP to process the insertion and deletion of form data, and provide relevant code examples.

1. Form data insertion
When the user fills out the form and clicks the "Submit" button, the form data will be sent to the server for processing. During processing, we need to extract the various fields from the form and insert them into the database. The following is a code example:

<?php
// 连接到数据库
$host = "localhost";
$username = "root";
$password = "123456";
$dbname = "mydatabase";
$conn = new mysqli($host, $username, $password, $dbname);
if ($conn->connect_error) {
    die("数据库连接失败:" . $conn->connect_error);
}

// 提取表单数据
$name = $_POST["name"];
$age = $_POST["age"];
$email = $_POST["email"];

// 插入数据到数据库
$sql = "INSERT INTO users (name, age, email) VALUES ('$name', '$age', '$email')";
if ($conn->query($sql) === TRUE) {
    echo "数据插入成功";
} else {
    echo "数据插入失败:" . $conn->error;
}

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

The above code first establishes a connection with the database, and then obtains the field values ​​​​in the form through the $_POST super global array. Next, insert the extracted field values ​​into the database table using SQL's INSERT INTO statement. If the insertion is successful, "data insertion successful" is output, otherwise an error message is output. Finally close the database connection.

2. Form data deletion
Sometimes we need to delete one or more records from the database. The following is a code example to delete form data:

<?php
// 连接到数据库
$host = "localhost";
$username = "root";
$password = "123456";
$dbname = "mydatabase";
$conn = new mysqli($host, $username, $password, $dbname);
if ($conn->connect_error) {
    die("数据库连接失败:" . $conn->connect_error);
}

// 提取表单数据
$id = $_POST["id"];

// 删除数据
$sql = "DELETE FROM users WHERE id = '$id'";
if ($conn->query($sql) === TRUE) {
    echo "数据删除成功";
} else {
    echo "数据删除失败:" . $conn->error;
}

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

The above code also establishes a connection to the database and extracts the id value in the form. Then use SQL's DELETE FROM statement to delete the corresponding record from the database. If the deletion is successful, "data deletion successful" is output, otherwise an error message is output. Finally close the database connection.

Conclusion:
Through the above code examples, we can see that using PHP to process form data insertion and deletion operations is not complicated. By connecting to the database, extracting form data, and performing database operations through SQL statements, we can insert and delete form data. I hope this article can help you understand PHP form processing.

The above is the detailed content of PHP form processing: form data insertion and deletion. For more information, please follow other related articles on the PHP Chinese website!

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