Home  >  Article  >  Topics  >  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)

WBOY
WBOYforward
2021-12-17 18:47:284017browse

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

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>用户管理系统</h2>
<h3>用户管理</h3>
<a href="add_user.php">添加用户</a><br/>
<a href="show_user.php">查看用户</a>
<h3>部门管理</h3>
<a href="add_dept.php">添加部门</a><br/>
<a href="show_dept.php">查看部门</a>
<h3>用户组管理</h3>
<a href="add_usergroup.php">添加用户组</a><br/>
<a href="show_usergroup.php">查看用户组</a>
<h3>权限管理</h3>
<a href="add_fun.php">添加权限</a><br/>
<a href="show_fun.php">查看权限</a>
</body>
</html>

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;");//设置中文字符集
?>

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>添加用户</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>

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>查看用户</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>

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>修改用户</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>

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>";
?>

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>

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:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete