search
HomeBackend DevelopmentPHP ProblemLet'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.

  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:


    
    
    
    

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);
  • 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);
  1. Complete register.php script
<?php $dbHost = "localhost";
$dbUser = "root";
$dbPassword = "";
$dbName = "mydb";

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

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

    $insert_query = "INSERT INTO users (username, password, email) VALUES (&#39;$username&#39;, &#39;$password&#39;, &#39;$email&#39;)";
    mysqli_query($conn, $insert_query);

    echo "注册成功";
}
?>

    
    
    
    

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:

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>
    
        
        
             
  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);
}
?>

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 "更新成功!";
}
?>

    
    
    

3. Delete function

  1. Write HTML

Add in HTML page Remove links or buttons.

<h1 id="用户列表">用户列表</h1>

                                                                                                                                                                                                                                                       
用户名邮箱操作
user1user1@example.com删除
user2user2@example.com删除
user3user3@example.com删除
  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);
?>

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
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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

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

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.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

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.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

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

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

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

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.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

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

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

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

See all articles

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

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