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!
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);
System implementation
The list of all page files is as follows:
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:
Effect :
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'");//设置中文字符集 ?>
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:
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:
When the addition is successful, the page will automatically jump to the following web page
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.
Effect:
Click the view button and you will jump to the following page
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:
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 "用户修改失败!
"; ?>
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.
When the deletion is successful, you will jump to the following page
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!