


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:
<form action="register.php" method="post"> <label>用户名:<input type="text" name="username"></label><br> <label>密码:<input type="password" name="password"></label><br> <label>邮箱:<input type="text" name="email"></label><br> <button type="submit" name="submit">注册</button> </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
<form action="register.php" method="post"> <label>用户名:<input type="text" name="username"></label><br> <label>密码:<input type="password" name="password"></label><br> <label>邮箱:<input type="text" name="email"></label><br> <button type="submit" name="submit">注册</button> </form>
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:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <h1>用户登录</h1> <form action="login.php" method="post"> <label>用户名:<input type="text" name="username"></label><br> <label>密码:<input type="password" name="password"></label><br> <button type="submit" name="submit">登录</button> </form> </body> </html>
When the user successfully logs in, he will be redirected to a page to modify information.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>修改信息</title> </head> <body> <h1>修改信息</h1> <form action="update.php" method="post"> <label>新的密码:<input type="password" name="password"></label><br> <label>新的邮箱:<input type="text" name="email"></label><br> <button type="submit" name="submit">保存</button> </form> </body> </html>
- 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 "更新成功!"; } ?> <form action="update.php" method="post"> <label>新的密码:<input type="password" name="password"></label><br> <label>新的邮箱:<input type="text" name="email"></label><br> <button type="submit" name="submit">保存</button> </form>
3. Delete function
- Write HTML
Add in HTML page Remove links or buttons.
<h1>用户列表</h1> <table> <thead> <tr> <th>用户名</th> <th>邮箱</th> <th>操作</th> </tr> </thead> <tbody> <tr> <td>user1</td> <td>user1@example.com</td> <td><a href="delete.php?id=1">删除</a></td> </tr> <tr> <td>user2</td> <td>user2@example.com</td> <td><a href="delete.php?id=2">删除</a></td> </tr> <tr> <td>user3</td> <td>user3@example.com</td> <td><a href="delete.php?id=3">删除</a></td> </tr> </tbody> </table>
- 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!

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

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.

Article discusses retrieving data from databases using PHP, covering steps, security measures, optimization techniques, and common errors with solutions.Character count: 159

The article discusses strategies to prevent CSRF attacks in PHP, including using CSRF tokens, Same-Site cookies, and proper session management.

The article discusses the mysqli_query() and mysqli_fetch_assoc() functions in PHP for MySQL database interactions. It explains their roles, differences, and provides a practical example of their use. The main argument focuses on the benefits of usin

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.
