本文實例講述了ThinkPHP框架實現(xiàn)用戶信息查詢更新及刪除功能。分享給大家供大家參考,具體如下:
一 代碼
1、配置文件
?php return array( 'APP_DEBUG' => false, // 關(guān)閉調(diào)試模式 'DB_TYPE'=> 'mysql', // 數(shù)據(jù)庫類型 'DB_HOST'=> 'localhost', // 數(shù)據(jù)庫服務(wù)器地址 'DB_NAME'=>'db_database30', // 數(shù)據(jù)庫名稱 'DB_USER'=>'root', // 數(shù)據(jù)庫用戶名 'DB_PWD'=>'root', // 數(shù)據(jù)庫密碼 'DB_PORT'=>'3306', // 數(shù)據(jù)庫端口 'DB_PREFIX'=>'think_', // 數(shù)據(jù)表前綴 ); ?>
2、入口文件
?php define('THINK_PATH', '../ThinkPHP'); //定義ThinkPHP框架路徑(相對于入口文件) define('APP_NAME', 'App'); //定義項目名稱 define('APP_PATH', './App'); //定義項目路徑 require(THINK_PATH."/ThinkPHP.php"); //加載框架入口文件 App::run(); //實例化一個網(wǎng)站應(yīng)用實例 ?>
3、控制器文件
?php header("Content-Type:text/html; charset=utf-8"); //設(shè)置頁面編碼格式 class IndexAction extends Action{ public function index(){ $db = M('User'); // 實例化模型類,參數(shù)數(shù)據(jù)表名稱,不包含前綴 $select = $db->order('id desc')->limit(10)->select(); $this->assign('select',$select); // 模板變量賦值 $this->display(); // 指定模板頁 } public function update(){ $db = M('User'); // 實例化模型類,參數(shù)數(shù)據(jù)表名稱,不包含前綴 $select = $db->where('id='.$_GET['id'])->select(); $this->assign('select',$select); // 模板變量賦值 $this->display(update); // 指定模板頁 if(isset($_POST['id'])){ $data['user'] = $_POST['user']; // 要修改的數(shù)據(jù)對象屬性賦值 $data['pass'] = md5($_POST['pass']); $data['address'] = $_POST['address']; $result=$db->where('id='.$_POST['id'])->save($data); // 根據(jù)條件保存修改的數(shù)據(jù) if($result){ $this->redirect('Index/index','', 2,'數(shù)據(jù)更新成功'); //頁面重定向 } } } public function delete(){ $db = M('User'); // 實例化模型類,參數(shù)數(shù)據(jù)表名稱,不包含前綴 $result=$db->where('id='.$_GET['id'])->delete(); // 刪除id為5的用戶數(shù)據(jù) if($result){ $this->redirect('Index/index','', 2,'數(shù)據(jù)刪除成功'); //頁面重定向 } } } ?>
4、模板文件一
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> html xmlns="http://www.w3.org/1999/xhtml"> head> meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> title>用戶信息輸出/title> link href="__ROOT__/Public/Css/style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" /> /head> body> table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF"> tr> td colspan="4" bgcolor="#FFFFFF" class="title" align="center">用戶信息/td> /tr> tr class="title"> td bgcolor="#FFFFFF" width="44">ID/td> td bgcolor="#FFFFFF" width="120">名稱/td> td bgcolor="#FFFFFF" width="111">地址/td> td bgcolor="#FFFFFF" width="111">操作/td> /tr> foreach name='select' item='user' > tr class="content"> td bgcolor="#FFFFFF">{$user.id}/td> td bgcolor="#FFFFFF">{$user.user}/td> td bgcolor="#FFFFFF">{$user.address}/td> td bgcolor="#FFFFFF">a href="__URL__/update?id={$user.id}" rel="external nofollow" >更新/a>/a href="__URL__/delete?id={$user.id}" rel="external nofollow" >刪除/a>/td> /tr> /foreach> /table> /body> /html>
5、模板文件二
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> html xmlns="http://www.w3.org/1999/xhtml"> head> meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> title>用戶信息輸出/title> link href="__ROOT__/Public/Css/style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" /> /head> body> form id="form2" name="form2" method="post" action="__URL__/update"> table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF"> tr> td colspan="2" bgcolor="#FFFFFF" class="title" align="center">用戶信息/td> /tr> foreach name='select' item='user' > tr class="content"> td bgcolor="#FFFFFF" class="right" width="103">名稱:/td> td bgcolor="#FFFFFF" width="289"> input type="hidden" name="id" id="hiddenField" value="{$user.id}" />input name="user" type="text" id="user" size="20" value="{$user.user}" />/td> /tr> tr class="content"> td bgcolor="#FFFFFF" class="right">密碼:/td> td bgcolor="#FFFFFF">input name="pass" type="password" id="pass" size="20" value="{$user.pass}" /> /td> /tr> tr class="content"> td bgcolor="#FFFFFF" class="right">nbsp;地址:/td> td bgcolor="#FFFFFF">nbsp; input name="address" type="text" id="address" size="30" value="{$user.address}" /> /td> /tr> tr class="content"> td bgcolor="#FFFFFF">nbsp;/td> td bgcolor="#FFFFFF">input type="submit" name="button" id="button" value="更新" />/td> /tr> /foreach> /table> /form> /body> /html>
二 運行結(jié)果
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。
標(biāo)簽:興安盟 濱州 太原 曲靖 南京 南昌 白酒營銷 株洲
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ThinkPHP框架實現(xiàn)用戶信息查詢更新及刪除功能示例》,本文關(guān)鍵詞 ThinkPHP,框架,實現(xiàn),用戶,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。