這篇文章主要介紹了ThinkPHP架構實現使用者資訊查詢更新及刪除功能,結合執行個體形式分析了thinkPHP架構資料庫配置、控制與模板調用實現資訊查詢、更新、刪除等功能相關操作技巧,需要的朋友可以參考下
本文執行個體講述了ThinkPHP架構實現使用者資訊查詢更新及刪除功能。分享給大家供大家參考,具體如下:
一 代碼
1、設定檔
<?phpreturn array( 'APP_DEBUG' => false, // 關閉偵錯模式 'DB_TYPE'=> 'mysql', // 資料庫類型 'DB_HOST'=> 'localhost', // 資料庫伺服器地址 'DB_NAME'=>'db_database30', // 資料庫名稱 'DB_USER'=>'root', // 資料庫使用者名稱 'DB_PWD'=>'root', // 資料庫密碼 'DB_PORT'=>'3306', // 資料庫連接埠 'DB_PREFIX'=>'think_', // 資料表首碼);?>
2、入口檔案
<?phpdefine('THINK_PATH', '../ThinkPHP'); //定義ThinkPHP架構路徑(相對於入口檔案)define('APP_NAME', 'App'); //定義項目名稱define('APP_PATH', './App'); //定義項目路徑require(THINK_PATH."/ThinkPHP.php"); //載入架構入口檔案App::run(); //執行個體化一個網站應用程式執行個體?>
3、控制器檔案
<?phpheader("Content-Type:text/html; charset=utf-8"); //設定頁面編碼格式class IndexAction extends Action{ public function index(){ $db = M('User'); // 執行個體化模型類,參數資料表名稱,不包含首碼 $select = $db->order('id desc')->limit(10)->select(); $this->assign('select',$select); // 模板變數賦值 $this->display(); // 指定模板頁 } public function update(){ $db = M('User'); // 執行個體化模型類,參數資料表名稱,不包含首碼 $select = $db->where('id='.$_GET['id'])->select(); $this->assign('select',$select); // 模板變數賦值 $this->display(update); // 指定模板頁 if(isset($_POST['id'])){ $data['user'] = $_POST['user']; // 要修改的資料對象屬性賦值 $data['pass'] = md5($_POST['pass']); $data['address'] = $_POST['address']; $result=$db->where('id='.$_POST['id'])->save($data); // 根據條件儲存修改的資料 if($result){ $this->redirect('Index/index','', 2,'資料更新成功'); //頁面重新導向 } } } public function delete(){ $db = M('User'); // 執行個體化模型類,參數資料表名稱,不包含首碼 $result=$db->where('id='.$_GET['id'])->delete(); // 刪除id為5的使用者資料 if($result){ $this->redirect('Index/index','', 2,'資料刪除成功'); //頁面重新導向 } }}?>
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"> 地址:</td> <td bgcolor="#FFFFFF"> <input name="address" type="text" id="address" size="30" value="{$user.address}" /> </td> </tr> <tr class="content"> <td bgcolor="#FFFFFF"> </td> <td bgcolor="#FFFFFF"><input type="submit" name="button" id="button" value="更新" /></td> </tr> </foreach></table></form></body></html>
二 運行結果
以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!