這篇文章給大家分享的是有關(guān)ThinkPHP框架如何實(shí)現(xiàn)用戶信息查詢(xún)更新及刪除功能的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

具體如下:
一 代碼
1、配置文件
<?php return array( 'APP_DEBUG' => false, // 關(guān)閉調(diào)試模式 'DB_TYPE'=> 'mysql', // 數(shù)據(jù)庫(kù)類(lèi)型 'DB_HOST'=> 'localhost', // 數(shù)據(jù)庫(kù)服務(wù)器地址 'DB_NAME'=>'db_database30', // 數(shù)據(jù)庫(kù)名稱(chēng) '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)目名稱(chē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í)例化模型類(lèi),參數(shù)數(shù)據(jù)表名稱(chēng),不包含前綴
$select = $db->order('id desc')->limit(10)->select();
$this->assign('select',$select); // 模板變量賦值
$this->display(); // 指定模板頁(yè)
}
public function update(){
$db = M('User'); // 實(shí)例化模型類(lèi),參數(shù)數(shù)據(jù)表名稱(chēng),不包含前綴
$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í)例化模型類(lèi),參數(shù)數(shù)據(jù)表名稱(chēng),不包含前綴
$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">名稱(chēng)</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">名稱(chēng):</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>二 運(yùn)行結(jié)果

感謝各位的閱讀!關(guān)于“ThinkPHP框架如何實(shí)現(xiàn)用戶信息查詢(xún)更新及刪除功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
網(wǎng)頁(yè)名稱(chēng):ThinkPHP框架如何實(shí)現(xiàn)用戶信息查詢(xún)更新及刪除功能-創(chuàng)新互聯(lián)
標(biāo)題來(lái)源:http://www.chinadenli.net/article18/iehdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷(xiāo)推廣、App開(kāi)發(fā)、品牌網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、建站公司、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容