本文實(shí)例講述了ThinkPHP框架實(shí)現(xiàn)用戶信息查詢更新及刪除功能。分享給大家供大家參考,具體如下:
一 代碼
1、配置文件
?php
return array(
'APP_DEBUG' => false, // 關(guān)閉調(diào)試模式
'DB_TYPE'=> 'mysql', // 數(shù)據(jù)庫(kù)類型
'DB_HOST'=> 'localhost', // 數(shù)據(jù)庫(kù)服務(wù)器地址
'DB_NAME'=>'db_database30', // 數(shù)據(jù)庫(kù)名稱
'DB_USER'=>'root', // 數(shù)據(jù)庫(kù)用戶名
'DB_PWD'=>'root', // 數(shù)據(jù)庫(kù)密碼
'DB_PORT'=>'3306', // 數(shù)據(jù)庫(kù)端口
'DB_PREFIX'=>'think_', // 數(shù)據(jù)表前綴
);
?>
2、入口文件
?php
define('THINK_PATH', '../ThinkPHP'); //定義ThinkPHP框架路徑(相對(duì)于入口文件)
define('APP_NAME', 'App'); //定義項(xiàng)目名稱
define('APP_PATH', './App'); //定義項(xiàng)目路徑
require(THINK_PATH."/ThinkPHP.php"); //加載框架入口文件
App::run(); //實(shí)例化一個(gè)網(wǎng)站應(yīng)用實(shí)例
?>
3、控制器文件
?php
header("Content-Type:text/html; charset=utf-8"); //設(shè)置頁(yè)面編碼格式
class IndexAction extends Action{
public function index(){
$db = M('User'); // 實(shí)例化模型類,參數(shù)數(shù)據(jù)表名稱,不包含前綴
$select = $db->order('id desc')->limit(10)->select();
$this->assign('select',$select); // 模板變量賦值
$this->display(); // 指定模板頁(yè)
}
public function update(){
$db = M('User'); // 實(shí)例化模型類,參數(shù)數(shù)據(jù)表名稱,不包含前綴
$select = $db->where('id='.$_GET['id'])->select();
$this->assign('select',$select); // 模板變量賦值
$this->display(update); // 指定模板頁(yè)
if(isset($_POST['id'])){
$data['user'] = $_POST['user']; // 要修改的數(shù)據(jù)對(duì)象屬性賦值
$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ù)更新成功'); //頁(yè)面重定向
}
}
}
public function delete(){
$db = M('User'); // 實(shí)例化模型類,參數(shù)數(shù)據(jù)表名稱,不包含前綴
$result=$db->where('id='.$_GET['id'])->delete(); // 刪除id為5的用戶數(shù)據(jù)
if($result){
$this->redirect('Index/index','', 2,'數(shù)據(jù)刪除成功'); //頁(yè)面重定向
}
}
}
?>
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>
二 運(yùn)行結(jié)果

更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- Thinkphp5.0框架使用模型Model的獲取器、修改器、軟刪除數(shù)據(jù)操作示例
- Thinkphp5.0 框架使用模型Model添加、更新、刪除數(shù)據(jù)操作詳解
- tp5(thinkPHP5框架)使用DB實(shí)現(xiàn)批量刪除功能示例
- thinkPHP利用ajax異步上傳圖片并顯示、刪除的示例
- ThinkPHP刪除欄目(實(shí)現(xiàn)批量刪除欄目)
- 基于php(Thinkphp)+jquery 實(shí)現(xiàn)ajax多選反選不選刪除數(shù)據(jù)功能
- thinkphp框架實(shí)現(xiàn)刪除和批量刪除
- thinkPHP刪除前彈出確認(rèn)框的簡(jiǎn)單實(shí)現(xiàn)方法
- 基于ThinkPHP刪除目錄及目錄文件函數(shù)