Home >Backend Development >PHP Problem >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.
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.
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.
Use the mysqli_connect() function provided by PHP.
$conn = mysqli_connect($dbHost, $dbUser, $dbPassword, $dbName);
Use PHP's $_POST method to get form data.
$username = $_POST['username']; $password = $_POST['password']; $email = $_POST['email'];
Insert the obtained data into the table.
$insert_query = "INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')"; mysqli_query($conn, $insert_query);
<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
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>
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
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>
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!