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

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 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)

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

PHP: Is It Dying or Simply Adapting? PHP: Is It Dying or Simply Adapting? Apr 11, 2025 am 12:13 AM

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

See all articles