Table of Contents
用户管理系统
用户管理
部门管理
用户组管理
权限管理
添加用户
查看用户
修改用户
Home Topics php mysql How to use PHP+Mysql to implement basic add, delete, modify and query functions? (detailed examples)

How to use PHP+Mysql to implement basic add, delete, modify and query functions? (detailed examples)

Dec 17, 2021 pm 06:47 PM
mysql php

In this article, we will take a look at how to use mysql to implement simple add, delete, modify, and query functions. In this article, we need to create multiple pages to process the data in the database. I hope it will be helpful to everyone!

How to use PHP+Mysql to implement basic add, delete, modify and query functions? (detailed examples)

PHP is an object-oriented, interpreted scripting language embedded in HTML documents that is executed on the server side. The language style is similar to the C language. It has powerful functions, can realize all CGI (Common Gateway Interface, a tool for "talking" between server and client programs) functions, and has a faster execution speed than ordinary CGI.

The following connection operations are under the WAMP platform environment. If you have friends who have not yet deployed the environment, you can refer to the following link: http://www.imooc.com/learn/54 which is explained in detail in the second chapter of the video.

Create database

Because we need to connect to the Mysql database, here we first create a database named db_user

--创建数据库db_user
create database db_user;
--指定当前数据库为db_user
use db_user;
--用户信息表users
create table users
(
user_id int not null auto_increament primary key,
user_name char(10) not null,
user_psw char(10) not null,
user_sex char(1) not null,
user_age int null,
user_dept int not null,
user_group int not null
);
--部门表dept
create table dept
(
dept_id int not null auto_increment primary key,
dept_name char(20) not null,
dept_leader char(10) not null,
dept_location char(50) not null
);
--用户组表usergroup
create table usergroup
(
group_id int not null auto_increment primary key,
group_name char(20) not null,
group_desc char(50) not null
);
--权限表func
create table func
(
func_id int not null auto_increment primary key,
func_name char(20) not null,
func_link char(20) not null
);
--用户组权限表groupfunc
create table groupfunc
(
id int not null auto_increment primary key,
group_id int not null,
func_id int not null
);
--插入一条测试数据
insert into db_user.users(`user_id`, `user_name`, `user_psw`, `user_sex`, `user_age`, `user_dept`, `user_group`) values (2, '隔壁老王', '2396', '男', 33, 0, 1);
Copy after login

System implementation

The list of all page files is as follows:

How to use PHP+Mysql to implement basic add, delete, modify and query functions? (detailed examples)

Next, just one step Explain the functions and implementation of each page file step by step.

1. Main page

Create the main page file index.html of the system, and the implementation code is as follows:

<html>
<head>
<title>一个简单用户管理系统实例</title>
</head>
<body>
<h2 id="用户管理系统">用户管理系统</h2>
<h3 id="用户管理">用户管理</h3>
<a href="add_user.php">添加用户</a><br/>
<a href="show_user.php">查看用户</a>
<h3 id="部门管理">部门管理</h3>
<a href="add_dept.php">添加部门</a><br/>
<a href="show_dept.php">查看部门</a>
<h3 id="用户组管理">用户组管理</h3>
<a href="add_usergroup.php">添加用户组</a><br/>
<a href="show_usergroup.php">查看用户组</a>
<h3 id="权限管理">权限管理</h3>
<a href="add_fun.php">添加权限</a><br/>
<a href="show_fun.php">查看权限</a>
</body>
</html>
Copy after login

Effect :

How to use PHP+Mysql to implement basic add, delete, modify and query functions? (detailed examples)

2. Common code module

Create a new common.php file with the following code. Connect to the database server. Here we encapsulate the operation of connecting to the database into a common code module, which is introduced in each page file below through , so that there is no need to write the connection code repeatedly.

<?php
$con=mysql_connect("localhost:3306","root","642765") or die("数据库服务器连接失败!<br>");
mysql_select_db("db_user",$con) or die("数据库选择失败!<br>");
mysql_query("set names &#39;gbk&#39;");//设置中文字符集
?>
Copy after login

In PHP, you can use the following two functions to establish a connection with the Mysql database server,

mysql_connect(): Establish a non-persistent connection

mysql_pconnect(): Establishing a persistent connection

A non-persistent connection is established here.

3. Design and implementation of each page

Add user

The implementation code for adding the user's web page file add_user.php is as follows:

<?php require_once "common.php";?>
<html>
<head>
<title>添加用户</title>
</head>
<body>
<h3 id="添加用户">添加用户</h3>
<form id="add_user" name="add_user" method="post" action="insert_user.php">
用户姓名:<input type="text" name="user_name"/><br/>
用户口令:<input type="text" name="user_psw"/><br/>
用户性别:<input type="text" name="user_sex"/><br/>
用户年龄:<input type="text" name="user_age"/><br/>
所属部门:<select name="show_user_name">
<?php
$sql="select * from dept";
$result=mysql_query($sql,$con);
while($rows=mysql_fetch_row($result)){
echo "<option value=".$rows[0].">".$rows[1]."</option>";
}
?>
</select><br/>
用户组名:<select name="user_group">
    <?php
    $sql="select * from usergroup";
    $result=mysql_query($sql,$con);
    while($rows=mysql_fetch_row($result)){
        echo "<option value=".$rows[0].">".$rows[1]."</option>";
    }
    ?>
    </select><br/>
    <br/>
<input type="submit" value="添加"/>
</form>
</body>
</html>
Copy after login

Then, deploy the program in the enabled wamp platform environment, and enter "http://localhost: Port number/file path" to see the effect. You may have discovered from the website that my port number is 8080, which is customized by me. The default port number is 80 (in this case, you don’t need to write the port number, just localhost).

Effect:

How to use PHP+Mysql to implement basic add, delete, modify and query functions? (detailed examples)

When the addition is successful, the page will automatically jump to the following web page

How to use PHP+Mysql to implement basic add, delete, modify and query functions? (detailed examples)

View users

The implementation code for viewing the user's web page file show_user.php is as follows. You can view the user by specifying the user name or the department to which the user belongs. All personal information.

<?php require_once "common.php";?>
<html>
<head><title>查看用户</title>
</head>
<body>
<h3 id="查看用户">查看用户</h3>
<form id="show_user" name="show_user" method="post" action="select_user.php">
用户姓名:<input type="text" name="show_user_name"/><br/>
所属部门:<select name="show_user_dept">
<option value=0>所有部门</option>
<?php
$sql="select * from dept";
$result=mysql_query($sql,$con);
while($rows=mysql_fetch_row($result)){
echo "<option value=".$rows[0].">".$rows[1]."</option>";
}
?>
</select><br/>
<br/>
<input type="submit" value="查看"/>
</form>
</body>
</html>
Copy after login

Effect:

How to use PHP+Mysql to implement basic add, delete, modify and query functions? (detailed examples)

Click the view button and you will jump to the following page

How to use PHP+Mysql to implement basic add, delete, modify and query functions? (detailed examples)

As can be seen from the figure, the user's view results page contains hyperlink entries for modifying the user and deleting the user, corresponding to the change_user.php and delete_user.php files respectively.

Modify user

The implementation code for modifying the user's web page file change_user.php is as follows:

<?php require_once "common.php";?>
<html>
<head><title>修改用户</title>
</head>
<body>
    <h3 id="修改用户">修改用户</h3>
    <form id="add_user" name="add_user" method="post" action="update_user.php?user_id=
        <?php echo trim($_GET[&#39;user_id&#39;]);?>" >
    用户姓名:<input type="text" name="user_name"/><br/>
    用户口令:<input type="text" name="user_psw"/><br/>
    用户性别:<input type="text" name="user_sex"/><br/>
    用户年龄:<input type="text" name="user_age"/><br/>
    所属部门:<select name="user_dept">
    <option value=0>请选择部门</option>
    <?php
    $sql="select * from dept";
    $result=mysql_query($sql,$con);
    while($rows=mysql_fetch_row($result)){
        echo "<option value=".$rows[0].">".$rows[1]."</option>";
    }
    ?>
    </select><br/>
用户组名:<select name="user_group">
    <option value=0>请选择用户组</option>
    <?php
    $sql="select * from usergroup";
    $result=mysql_query($sql,$con);
    while($rows=mysql_fetch_row($result)) {
        echo "<option value=".$row[0].">".$rows[1]."</option>";
    }
    ?>
    </select><br/>
<br/>
<input type="submit" value="修改用户信息"/>
</form>
</body>
</html>
Copy after login

How to use PHP+Mysql to implement basic add, delete, modify and query functions? (detailed examples)

After entering the new user information on the above page, click the button to call the business logic processing code update_user.php in the application layer for performing user modification operations. The code content is as follows:

<?php require_once "common.php";
$user_id=trim($_GET[&#39;user_id&#39;]);
$user_name=trim($_POST[&#39;user_name&#39;]);
$user_psw=trim($_POST[&#39;user_psw&#39;]);
$user_sex=trim($_POST[&#39;user_sex&#39;]);
$user_age=trim($_POST[&#39;user_age&#39;]);
$user_dept=trim($_POST[&#39;user_dept&#39;]);
$user_group=trim($_POST[&#39;user_group&#39;]);
$sql="UPDATE users SET user_name=&#39;".$user_name."&#39;,user_psw=&#39;".$user_psw."&#39;,user_sex=&#39;".$user_sex."&#39;,user_age=&#39;".$user_age."&#39;,user_dept=&#39;".$user_dept."&#39;,user_group=&#39;".$user_group."&#39;  WHERE user_id=";
$sql=$sql.$user_id;
if(mysql_query($sql,$con))
    echo "用户修改成功!<br>";
else
    echo "用户修改失败!<br>";
?>
Copy after login

Delete user

In the user view results page, there is a hyperlink to delete the user. Click to call the following logical processing code delete_user .php to delete the current user.

<?php require_once "common.php";?>
<html>
<head><title>删除用户</title>
</head>
<body>
    <?php
    $user_id=trim($_GET[&#39;user_id&#39;]);
    $sql="DELETE FROM users WHERE user_id=";
    $sql=$sql.$user_id;
    if(mysql_query($sql,$con))
        echo "用户删除成功!<br>";
    else
        echo "用户删除失败!<br>";
    ?>
</body>
</html>
Copy after login

When the deletion is successful, you will jump to the following page

How to use PHP+Mysql to implement basic add, delete, modify and query functions? (detailed examples)

If you are interested, you can click on "PHP Video Tutorial" to learn more about PHP knowledge.

The above is the detailed content of How to use PHP+Mysql to implement basic add, delete, modify and query functions? (detailed examples). 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

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

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 and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

See all articles