PHP connects to Mysql to realize basic addition, deletion, modification, and query-user management system

Preface

Recently I was learning PHP and Mysql, while reading a book and typing the code to try to complete a simple user management system, but realized some very simple operations, although it is a bit low (automatically shield the big cow haha), but I think it is better to summarize, Can deepen the impression, please point out the wrong place, study together and communicate together.

Overview

PHP is an object-oriented and interpreted scripting language embedded in HTML documents executed on the server side. The language style is similar to the C language. It has powerful functions, can realize all the functions of CGI (common gateway interface, a tool for "talking" between server and client program), and has a faster execution speed than general CGI. 
The following connection operation is in the WAMP platform environment. If you have a small partner who has not yet deployed the environment, you can refer to the following link: http://www.imooc.com/learn/54 There is a detailed explanation in the second chapter of the video.

Create database

Because we want to connect to the Mysql database, we will first create a database named db_user

--Create database db_usercreate database db_user;--Specify the current database as db_useruse db_user;--User information table userscreate 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);---department table 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);-- permission table funccreate table func
(
func_id int not null auto_increment primary key,
func_name char(20) not null,
func_link char(20) not null);--User group permission table groupfunc create table groupfunc
(
id int not null auto_increment primary key,
group_id int not null,
func_id int not null);--Insert a piece of test data insert into db_user.users(`user_id`, `user_name`, `user_psw`, `user_sex`, `user_age`, `user_dept`, `user_group`) values ​​(2, 'President next door', '2396','male', 33, 0, 1); 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849

System implementation

The list of all page files is as follows:

Write picture description here

Next, explain the function and realization of each page file step by step.

1. Main page

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

<html><head><title>An example of a simple user management system</title></head><body><h2>User Management System</h2><h3>User Management</h3><a href=" add_user.php">Add User</a><br/><a href="show_user.php">View User</a><h3>Department Management</h3><a href="add_dept.php"> Add department</a><br/><a href="show_dept.php">View department</a><h3>User group management</h3><a href="add_usergroup.php">Add user group< /a><br/><a href="show_usergroup.php">View user groups</a><h3>Permission management</h3><a href="add_fun.php">Add permissions</a>< br/><a href="show_fun.php">View permissions</a></body></html>1234567891011121314151617181920212223

effect:

Write picture description here

2. Common code module

Create a new common.php file, the code is as follows, to connect to the database server, here we encapsulate the operation of connecting to the database into a common code module, introduced in the following page files <?php require_once "common.php";?> , so that there is no need to repeatedly write connection code.

<?php$con=mysql_connect("localhost:3306","root","642765") or die("Database server connection failed!<br>");
mysql_select_db("db_user",$con) or die("Database selection failed!<br>");
mysql_query("set names'gbk'");//Set Chinese character set?>12345

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(): establish a persistent connection 
Here is a non-persistent connection.

3. Design and implementation of each page

Add user

The implementation code of the web page file add_user.php for adding users 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>123456789101112131415161718192021222324252627282930313233343536

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

Write picture description here

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

Write picture description here

View users

The implementation code of viewing the user's web page file show_user.php is as follows, you can view all the personal information of the user by specifying the user's name or the user's department.

<?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>1234567891011121314151617181920212223

effect:

Write picture description here

Click the view button, it will jump to the following page

Write picture description here

It can be seen from the figure that the user’s view result page contains hyperlink entries for performing operations to modify the user and delete 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>Edit user</h3>
    <form id="add_user" name="add_user" method="post" action="update_user.php?user_id=
        <?php echo trim($_GET['user_id']);?>" >
    用户姓名:<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/>
    Department: <select name="user_dept">
    <option value=0>Please select a department</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>Please select a user group</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>1234567891011121314151617181920212223242526272829303132333435363738

Write picture description here

After entering the new user information in the above page, click the button to call update_user.php, the business logic processing code used to modify the user operation in the application layer. The code content is as follows:

<?php require_once "common.php";$user_id=trim($_GET['user_id']);$user_name=trim($_POST['user_name']);$user_psw=trim($_POST['user_psw']);$user_sex=trim($_POST['user_sex']);$user_age=trim($_POST['user_age']);$user_dept=trim($_POST['user_dept']);$user_group=trim($_POST['user_group']);$sql="UPDATE users SET user_name='".$user_name."',user_psw='".$user_psw."',user_sex='".$user_sex."',user_age='".$user_age."',user_dept='".$user_dept."',user_group='".$user_group."'  WHERE user_id=";$sql=$sql.$user_id;if(mysql_query($sql,$con))
    echo "User modified successfully!<br>";else
    echo "User modification failed!<br>";
?>123456789101112131415

delete users

In the user view result page, there is a hyperlink to delete the user. Click to call the following logic 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['user_id']);    $sql="DELETE FROM users WHERE user_id=";    $sql=$sql.$user_id;    if(mysql_query($sql,$con))        echo "用户删除成功!<br>";    else
        echo "User deletion failed!<br>"; ?></body></html>12345678910111213141516

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

Write picture description here

Finish

At this point, a simple system to connect to the Mysql database through PHP is complete.

Guess you like

Origin blog.csdn.net/mjian178/article/details/112691352