亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

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

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

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

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

    下面的連接操作是在WAMP平臺環(huán)境下的。如果有還沒部署環(huán)境的小伙伴可以參考下面鏈接:http://www.imooc.com/learn/54在視頻的第二章有詳細(xì)講解。

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

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

    --創(chuàng)建數(shù)據(jù)庫db_user create database db_user; --指定當(dāng)前數(shù)據(jù)庫為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 ); --插入一條測試數(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)

    所有頁面文件列表如下:

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

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

    1.主頁面

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

    <html> <head> <title>一個(gè)簡單用戶管理系統(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ù)庫服務(wù)器,這里我們把連接數(shù)據(jù)庫的操作封裝成一個(gè)公共代碼模塊,在下面各頁面文件中通過<?php require_once "common.php";?> 引入,這樣就不用重復(fù)編寫連接代碼了。

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

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

    mysql_connect():建立非持久連接

    mysql_pconnect():建立持久連接

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

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

    添加用戶

    添加用戶的web頁面文件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>

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

    效果:

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

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

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

    查看用戶

    查看用戶的web頁面文件show_user.php的實(shí)現(xiàn)代碼如下,可以通過指定用戶姓名或用戶所屬部門來查看該用戶的全部個(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)擊查看按鈕,即會跳轉(zhuǎn)到下面頁面

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

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

    修改用戶

    修改用戶的web頁面文件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>請選擇部門</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>

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

    當(dāng)在上面頁面中輸入完新的用戶信息后,點(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é)果頁面中,有個(gè)刪除用戶的超鏈接,點(diǎn)擊即可調(diào)用下面的邏輯處理代碼delete_user.php,從而實(shí)現(xiàn)對當(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)刪除成功后,會跳轉(zhuǎn)到下面頁面

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

    大家如果感興趣的話,可以點(diǎn)擊《PHP視頻教程》進(jìn)行

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號