


Let's talk about how to implement registration, modification and deletion functions in PHP
In the field of web development, PHP has become one of the most popular server-side scripting languages. Its flexibility, efficiency, and ease of learning and use enable PHP to quickly and reliably create a variety of dynamic websites and web applications.
In actual development work, user registration, information modification and data deletion are essential functions. On PHP, the process of completing these common functions is relatively simple. Therefore, this article will introduce how to implement the registration, modification and deletion functions in PHP.
1. Registration function
1. Create a table
First you need to create a user data table, which can be created using a relational database management system such as MySQL and MariaDB.
The SQL statement to create a user table is as follows:
CREATE TABLE IF NOT EXISTS users ( id INT(11) NOT NULL AUTO_INCREMENT, username VARCHAR(100) NOT NULL, password VARCHAR(100) NOT NULL, email VARCHAR(100) NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The above table has an auto-increment ID field, as well as three fields: username, password and email address.
- Writing HTML
In the HTML page, we need to add a form to allow users to fill in registration information.
The following is a simple example form:
The form submits data to the page register.php. When a user clicks the "Register" button, data is sent to a background script for processing.
- PHP script
The main task of the PHP script is to process the input data and store it in the database. Here are the steps to implement.
- Establish a database connection
Use the mysqli_connect() function provided by PHP.
$conn = mysqli_connect($dbHost, $dbUser, $dbPassword, $dbName);
- Get form data
Use PHP's $_POST method to get form data.
$username = $_POST['username']; $password = $_POST['password']; $email = $_POST['email'];
- Storing data
Insert the obtained data into the table.
$insert_query = "INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')"; mysqli_query($conn, $insert_query);
- Complete register.php script
<?php $dbHost = "localhost"; $dbUser = "root"; $dbPassword = ""; $dbName = "mydb"; $conn = mysqli_connect($dbHost, $dbUser, $dbPassword, $dbName); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $username = $_POST['username']; $password = $_POST['password']; $email = $_POST['email']; $insert_query = "INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')"; mysqli_query($conn, $insert_query); echo "注册成功"; } ?>
2. Modify function
- Write HTML
User You need to log in to modify information, so you need to add a login interface and login form.
The login interface and login form sample code is as follows:
nbsp;html> <meta> <title>登录</title> <h1 id="用户登录">用户登录</h1>
When the user successfully logs in, he will be redirected to a page to modify information.
nbsp;html> <meta> <title>修改信息</title> <h1 id="修改信息">修改信息</h1>
- Write PHP script
Verify in login.php whether the user password is correct. If correct, store the information in PHP's $_SESSION variable and redirect to update.php page.
The following is the code of the login.php script:
<?php session_start(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $username = $_POST['username']; $password = $_POST['password']; $dbHost = "localhost"; $dbUser = "root"; $dbPassword = ""; $dbName = "mydb"; $conn = mysqli_connect($dbHost, $dbUser, $dbPassword, $dbName); $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { $_SESSION['username'] = $username; header('Location: update.php'); } else { echo "用户名或密码错误"; } mysqli_close($conn); } ?>
In the update.php page, get the username in the session, and then update the password and email address in the database.
The following is the code of update.php:
<?php session_start(); if (!isset($_SESSION['username'])) { header('Location: login.php'); } $dbHost = "localhost"; $dbUser = "root"; $dbPassword = ""; $dbName = "mydb"; $conn = mysqli_connect($dbHost, $dbUser, $dbPassword, $dbName); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $username = $_SESSION['username']; $password = $_POST['password']; $email = $_POST['email']; $update_query = "UPDATE users SET password='$password', email='$email' WHERE username='$username'"; mysqli_query($conn, $update_query); echo "更新成功!"; } ?>
3. Delete function
- Write HTML
Add in HTML page Remove links or buttons.
<h1 id="用户列表">用户列表</h1>
用户名 | 邮箱 | 操作 |
---|---|---|
user1 | user1@example.com | 删除 |
user2 | user2@example.com | 删除 |
user3 | user3@example.com | 删除 |
- Writing PHP script
The deletion operation requires obtaining the user ID and executing the SQL delete statement.
The following is the code of the delete.php script:
<?php $dbHost = "localhost"; $dbUser = "root"; $dbPassword = ""; $dbName = "mydb"; $conn = mysqli_connect($dbHost, $dbUser, $dbPassword, $dbName); if (isset($_GET['id'])) { $id = $_GET['id']; $delete_query = "DELETE FROM users WHERE id=$id"; mysqli_query($conn, $delete_query); echo "删除成功!"; } else { echo "删除失败!"; } mysqli_close($conn); ?>
Conclusion
As a common Web development language, PHP provides tools and methods to implement various functions. Including functions such as registration, modification and deletion. This article mainly introduces how to use PHP to implement these functions, hoping to be helpful to beginners and intermediate developers.
The above is the detailed content of Let's talk about how to implement registration, modification and deletion functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software