Home Backend Development PHP Problem Let's talk about how to implement registration, modification and deletion functions in PHP

Let's talk about how to implement registration, modification and deletion functions in PHP

Apr 26, 2023 am 10:31 AM

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;
Copy after login

The above table has an auto-increment ID field, as well as three fields: username, password and email address.

  1. 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>
Copy after login

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.

  1. 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);
Copy after login
  • Get form data

Use PHP's $_POST method to get form data.

$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
Copy after login
  • 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);
Copy after login
  1. 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>
Copy after login

2. Modify function

  1. 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>
Copy after login

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>
Copy after login
  1. 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[&#39;REQUEST_METHOD&#39;] === &#39;POST&#39;) {
    $username = $_POST[&#39;username&#39;];
    $password = $_POST[&#39;password&#39;];

    $dbHost = "localhost";
    $dbUser = "root";
    $dbPassword = "";
    $dbName = "mydb";

    $conn = mysqli_connect($dbHost, $dbUser, $dbPassword, $dbName);

    $sql = "SELECT * FROM users WHERE username=&#39;$username&#39; AND password=&#39;$password&#39;";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0) {
        $_SESSION['username'] = $username;
        header('Location: update.php');
    } else {
        echo "用户名或密码错误";
    }

    mysqli_close($conn);
}
?>
Copy after login

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[&#39;username&#39;])) {
    header(&#39;Location: login.php&#39;);
}

$dbHost = "localhost";
$dbUser = "root";
$dbPassword = "";
$dbName = "mydb";

$conn = mysqli_connect($dbHost, $dbUser, $dbPassword, $dbName);

if ($_SERVER[&#39;REQUEST_METHOD&#39;] === &#39;POST&#39;) {
    $username = $_SESSION[&#39;username&#39;];
    $password = $_POST[&#39;password&#39;];
    $email = $_POST[&#39;email&#39;];

    $update_query = "UPDATE users SET password=&#39;$password&#39;, email=&#39;$email&#39; WHERE username=&#39;$username&#39;";
    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>
Copy after login

3. Delete function

  1. 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>
Copy after login
  1. 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[&#39;id&#39;])) {
    $id = $_GET[&#39;id&#39;];

    $delete_query = "DELETE FROM users WHERE id=$id";
    mysqli_query($conn, $delete_query);

    echo "删除成功!";
} else {
    echo "删除失败!";
}

mysqli_close($conn);
?>
Copy after login

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. Mar 25, 2025 am 10:37 AM

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

PHP Encryption: Symmetric vs. asymmetric encryption. PHP Encryption: Symmetric vs. asymmetric encryption. Mar 25, 2025 pm 03:12 PM

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.

PHP Authentication & Authorization: Secure implementation. PHP Authentication & Authorization: Secure implementation. Mar 25, 2025 pm 03:06 PM

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

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities. OWASP Top 10 PHP: Describe and mitigate common vulnerabilities. Mar 26, 2025 pm 04:13 PM

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.

How do you retrieve data from a database using PHP? How do you retrieve data from a database using PHP? Mar 20, 2025 pm 04:57 PM

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

PHP CSRF Protection: How to prevent CSRF attacks. PHP CSRF Protection: How to prevent CSRF attacks. Mar 25, 2025 pm 03:05 PM

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

What is the purpose of mysqli_query() and mysqli_fetch_assoc()? What is the purpose of mysqli_query() and mysqli_fetch_assoc()? Mar 20, 2025 pm 04:55 PM

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

PHP Secure File Uploads: Preventing file-related vulnerabilities. PHP Secure File Uploads: Preventing file-related vulnerabilities. Mar 26, 2025 pm 04:18 PM

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.

See all articles