日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

本篇文章我們來(lái)看一下怎樣利用mysql來(lái)實(shí)現(xiàn)簡(jiǎn)單的增、刪、改、查的功能,其中需要?jiǎng)?chuàng)建多個(gè)頁(yè)面對(duì)數(shù)據(jù)庫(kù)的數(shù)據(jù)進(jìn)行處理,希望對(duì)大家有幫助!


怎樣利用PHP+Mysql實(shí)現(xiàn)基本的增刪改查功能?(實(shí)例詳解)


PHP是一種在服務(wù)器端執(zhí)行的嵌入HTML文檔的面向?qū)ο蟆⒔忉屝偷哪_本語(yǔ)言,語(yǔ)言風(fēng)格類似于c語(yǔ)言。它具有強(qiáng)大的功能,能實(shí)現(xiàn)所有的CGI(公共網(wǎng)關(guān)接口,服務(wù)器與客戶端程序進(jìn)行“交談”的一種工具)的功能,并比一般CGI有更快的執(zhí)行速度。

創(chuàng)建數(shù)據(jù)庫(kù)

因?yàn)橐B接Mysql數(shù)據(jù)庫(kù),所以這里我們就先建一個(gè)名叫db_user的數(shù)據(jù)庫(kù)

--創(chuàng)建數(shù)據(jù)庫(kù)db_user
create database db_user;
--指定當(dāng)前數(shù)據(jù)庫(kù)為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
);
--權(quán)限表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
);
--用戶組權(quán)限表groupfunc
create table groupfunc
(
id int not null auto_increment primary key,
group_id int not null,
func_id int not null
);
--插入一條測(cè)試數(shù)據(jù)
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);


系統(tǒng)實(shí)現(xiàn)

所有頁(yè)面文件列表如下:


怎樣利用PHP+Mysql實(shí)現(xiàn)基本的增刪改查功能?(實(shí)例詳解)


接下來(lái),就一步一步講解各個(gè)頁(yè)面文件的功能與實(shí)現(xiàn) 。

1.主頁(yè)面

創(chuàng)建系統(tǒng)的主頁(yè)面文件index.html,實(shí)現(xiàn)代碼如下:

<html>
<head>
    <title>一個(gè)簡(jiǎn)單用戶管理系統(tǒng)實(shí)例</title>
</head>
<body>
    <h2>用戶管理系統(tǒng)</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>權(quán)限管理</h3>
    <a href="add_fun.php">添加權(quán)限</a><br/>
    <a href="show_fun.php">查看權(quán)限</a>
</body>
</html>

效果:


怎樣利用PHP+Mysql實(shí)現(xiàn)基本的增刪改查功能?(實(shí)例詳解)

2.公共代碼模塊

新建common.php文件,代碼如下,用以連接數(shù)據(jù)庫(kù)服務(wù)器,這里我們把連接數(shù)據(jù)庫(kù)的操作封裝成一個(gè)公共代碼模塊,在下面各頁(yè)面文件中通過(guò)<?php require_once "common.php";?> 引入,這樣就不用重復(fù)編寫連接代碼了。

<?php
$con=mysql_connect("localhost:3306","root","642765") or die("數(shù)據(jù)庫(kù)服務(wù)器連接失敗!<br>");
mysql_select_db("db_user",$con) or die("數(shù)據(jù)庫(kù)選擇失敗!<br>");
mysql_query("set names 'gbk'");//設(shè)置中文字符集
?>

在PHP中,可以使用下面兩種函數(shù)來(lái)建立與Mysql數(shù)據(jù)庫(kù)服務(wù)器的連接,

mysql_connect():建立非持久連接

mysql_pconnect():建立持久連接

此處建立的是非持久連接。


3.各頁(yè)面的設(shè)計(jì)與實(shí)現(xiàn)

添加用戶

添加用戶的web頁(yè)面文件add_user.php的實(shí)現(xiàn)代碼如下:

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

然后,將程序部署在已開(kāi)啟的wamp平臺(tái)環(huán)境中,并在瀏覽器中輸入“http://localhost:端口號(hào)/文件路徑”,即可查看效果。大家從網(wǎng)址可能已經(jīng)發(fā)現(xiàn)我的端口號(hào)為8080,這是我自定義的,默認(rèn)的端口號(hào)是80(這時(shí)就可以不用寫端口號(hào),直接localhost)。

效果:

怎樣利用PHP+Mysql實(shí)現(xiàn)基本的增刪改查功能?(實(shí)例詳解)

當(dāng)添加成功后,頁(yè)面會(huì)自動(dòng)跳轉(zhuǎn)到下面的web頁(yè)面

怎樣利用PHP+Mysql實(shí)現(xiàn)基本的增刪改查功能?(實(shí)例詳解)

查看用戶

查看用戶的web頁(yè)面文件show_user.php的實(shí)現(xiàn)代碼如下,可以通過(guò)指定用戶姓名或用戶所屬部門來(lái)查看該用戶的全部個(gè)人信息。

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

效果:

怎樣利用PHP+Mysql實(shí)現(xiàn)基本的增刪改查功能?(實(shí)例詳解)

點(diǎn)擊查看按鈕,即會(huì)跳轉(zhuǎn)到下面頁(yè)面

怎樣利用PHP+Mysql實(shí)現(xiàn)基本的增刪改查功能?(實(shí)例詳解)

從圖中可以看出,在該用戶的查看結(jié)果頁(yè)面中包含了執(zhí)行修改該用戶和刪除該用戶操作的超鏈接入口,分別對(duì)應(yīng)著change_user.php和delete_user.php文件。

修改用戶

修改用戶的web頁(yè)面文件change_user.php的實(shí)現(xiàn)代碼如下:

<?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['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/>
    所屬部門:<select name="user_dept">
    <option value=0>請(qǐng)選擇部門</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>請(qǐng)選擇用戶組</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>

怎樣利用PHP+Mysql實(shí)現(xiàn)基本的增刪改查功能?(實(shí)例詳解)

當(dāng)在上面頁(yè)面中輸入完新的用戶信息后,點(diǎn)擊按鈕,即可調(diào)用應(yīng)用層中用于執(zhí)行修改用戶操作的業(yè)務(wù)邏輯處理代碼update_user.php,該代碼內(nèi)容如下:

<?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 "用戶修改成功!<br>";
else
    echo "用戶修改失敗!<br>";
?>

刪除用戶

在用戶查看結(jié)果頁(yè)面中,有個(gè)刪除用戶的超鏈接,點(diǎn)擊即可調(diào)用下面的邏輯處理代碼delete_user.php,從而實(shí)現(xiàn)對(duì)當(dāng)前用戶的刪除。

<?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 "用戶刪除失敗!<br>";
    ?>
</body>
</html>

當(dāng)刪除成功后,會(huì)跳轉(zhuǎn)到下面頁(yè)面


怎樣利用PHP+Mysql實(shí)現(xiàn)基本的增刪改查功能?(實(shí)例詳解)


分享到:
標(biāo)簽:PHP+Mysql 增刪改查功能
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定