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(); ?>
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(); ?>
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!