Home> Topics> php mysql> body text

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

WBOY
Release: 2022-03-30 12:01:10
forward
3769 people have browsed it

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:

  一个简单用户管理系统实例 

用户管理系统

用户管理

添加用户
查看用户

部门管理

添加部门
查看部门

用户组管理

添加用户组
查看用户组

权限管理

添加权限
查看权限
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.

"); mysql_select_db("db_user",$con) or die("数据库选择失败!
"); mysql_query("set names 'gbk'");//设置中文字符集 ?>
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:

   添加用户 

添加用户

用户姓名:
用户口令:
用户性别:
用户年龄:
所属部门:
用户组名:

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.

  查看用户 

查看用户

用户姓名:
所属部门:

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:

  修改用户 

修改用户

用户姓名:
用户口令:
用户性别:
用户年龄:
所属部门:
用户组名:

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:

"; else echo "用户修改失败!
"; ?>
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.

  删除用户 
"; else echo "用户删除失败!
"; ?>
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!

Related labels:
source:csdn.net
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!